|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/mark3labs/mcp-go/client" |
| 10 | + "github.com/mark3labs/mcp-go/mcp" |
| 11 | + "github.com/mark3labs/mcp-go/server" |
| 12 | +) |
| 13 | + |
| 14 | +// handleDummyTool is a simple tool that returns "foo bar" |
| 15 | +func handleDummyTool( |
| 16 | + ctx context.Context, |
| 17 | + request mcp.CallToolRequest, |
| 18 | +) (*mcp.CallToolResult, error) { |
| 19 | + return mcp.NewToolResultText("foo bar"), nil |
| 20 | +} |
| 21 | + |
| 22 | +func NewMCPServer() *server.MCPServer { |
| 23 | + mcpServer := server.NewMCPServer( |
| 24 | + "example-server", |
| 25 | + "1.0.0", |
| 26 | + server.WithResourceCapabilities(true, true), |
| 27 | + server.WithPromptCapabilities(true), |
| 28 | + server.WithToolCapabilities(true), |
| 29 | + ) |
| 30 | + mcpServer.AddTool(mcp.NewTool("dummy_tool", |
| 31 | + mcp.WithDescription("A dummy tool that returns foo bar"), |
| 32 | + ), handleDummyTool) |
| 33 | + |
| 34 | + return mcpServer |
| 35 | +} |
| 36 | + |
| 37 | +type MCPClient struct { |
| 38 | + client *client.Client |
| 39 | + serverInfo *mcp.InitializeResult |
| 40 | +} |
| 41 | + |
| 42 | +// NewMCPClient creates a new MCP client with an in-process MCP server. |
| 43 | +func NewMCPClient(ctx context.Context) (*MCPClient, error) { |
| 44 | + srv := NewMCPServer() |
| 45 | + client, err := client.NewInProcessClient(srv) |
| 46 | + if err != nil { |
| 47 | + return nil, fmt.Errorf("failed to create in-process client: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + // Start the client with timeout context |
| 51 | + ctxWithTimeout, cancel := context.WithTimeout(ctx, 5*time.Second) |
| 52 | + defer cancel() |
| 53 | + |
| 54 | + if err := client.Start(ctxWithTimeout); err != nil { |
| 55 | + return nil, fmt.Errorf("failed to start client: %w", err) |
| 56 | + } |
| 57 | + |
| 58 | + // Initialize the client |
| 59 | + initRequest := mcp.InitializeRequest{} |
| 60 | + initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION |
| 61 | + initRequest.Params.ClientInfo = mcp.Implementation{ |
| 62 | + Name: "Example MCP Client", |
| 63 | + Version: "1.0.0", |
| 64 | + } |
| 65 | + initRequest.Params.Capabilities = mcp.ClientCapabilities{} |
| 66 | + |
| 67 | + serverInfo, err := client.Initialize(ctx, initRequest) |
| 68 | + if err != nil { |
| 69 | + return nil, fmt.Errorf("failed to initialize MCP client: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + return &MCPClient{ |
| 73 | + client: client, |
| 74 | + serverInfo: serverInfo, |
| 75 | + }, nil |
| 76 | +} |
| 77 | + |
| 78 | +func main() { |
| 79 | + ctx := context.Background() |
| 80 | + client, err := NewMCPClient(ctx) |
| 81 | + if err != nil { |
| 82 | + log.Fatalf("Failed to create MCP client: %v", err) |
| 83 | + } |
| 84 | + |
| 85 | + toolsRequest := mcp.ListToolsRequest{} |
| 86 | + toolsResult, err := client.client.ListTools(ctx, toolsRequest) |
| 87 | + if err != nil { |
| 88 | + log.Fatalf("Failed to list tools: %v", err) |
| 89 | + } |
| 90 | + fmt.Println(toolsResult.Tools) |
| 91 | + |
| 92 | + request := mcp.CallToolRequest{} |
| 93 | + request.Params.Name = "dummy_tool" |
| 94 | + |
| 95 | + result, err := client.client.CallTool(ctx, request) |
| 96 | + if err != nil { |
| 97 | + log.Fatalf("Failed to call tool: %v", err) |
| 98 | + } |
| 99 | + fmt.Println(result.Content) |
| 100 | +} |
0 commit comments