Created
October 27, 2025 21:24
-
-
Save yakuter/a48385c9704304bc181882a266b099e3 to your computer and use it in GitHub Desktop.
go-mcp-sdk-client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import ( | |
| "context" | |
| "log" | |
| "os/exec" | |
| "github.com/modelcontextprotocol/go-sdk/mcp" | |
| ) | |
| func main() { | |
| ctx := context.Background() | |
| client := mcp.NewClient(&mcp.Implementation{Name: "my-client", Version: "v0.1.0"}, nil) | |
| transport := &mcp.CommandTransport{Command: exec.Command("line-counter-binary")} | |
| session, err := client.Connect(ctx, transport, nil) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| defer session.Close() | |
| params := &mcp.CallToolParams{ | |
| Name: "count_lines", | |
| Arguments: map[string]any{"file_path": "/tmp/foo.txt"}, | |
| } | |
| res, err := session.CallTool(ctx, params) | |
| if err != nil { | |
| log.Fatalf("CallTool failed: %v", err) | |
| } | |
| if res.IsError { | |
| log.Fatalf("Tool reported error") | |
| } | |
| for _, c := range res.Content { | |
| // extract typed content | |
| if o, ok := c.(*mcp.TextContent); ok { | |
| log.Print(o.Text) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment