After some experience with the flattened version of Content, we see that it can easily lead to incorrect usage and is a harder API to read. Therefore, this CL changes back to a design similar to what we had prior to https://go.dev/cl/672415, though opting to promote content unmarshalling to the protocol types that use it, which leads to an overall simpler api.
47 lines
1,001 B
Go
47 lines
1,001 B
Go
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
|
|
// Use of this source code is governed by an MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// !+
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"os/exec"
|
|
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
)
|
|
|
|
func main() {
|
|
ctx := context.Background()
|
|
|
|
// Create a new client, with no features.
|
|
client := mcp.NewClient("mcp-client", "v1.0.0", nil)
|
|
|
|
// Connect to a server over stdin/stdout
|
|
transport := mcp.NewCommandTransport(exec.Command("myserver"))
|
|
session, err := client.Connect(ctx, transport)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer session.Close()
|
|
|
|
// Call a tool on the server.
|
|
params := &mcp.CallToolParams{
|
|
Name: "greet",
|
|
Arguments: map[string]any{"name": "you"},
|
|
}
|
|
res, err := session.CallTool(ctx, params)
|
|
if err != nil {
|
|
log.Fatalf("CallTool failed: %v", err)
|
|
}
|
|
if res.IsError {
|
|
log.Fatal("tool failed")
|
|
}
|
|
for _, c := range res.Content {
|
|
log.Print(c.(*mcp.TextContent).Text)
|
|
}
|
|
}
|
|
|
|
//!-
|