Use the weave program from golang.org/x/example to weave together the README text with buildable programs. This ensures the code in the README compiles. Change-Id: Ib5e10c8f808cc6d1ed4addbee784607e910ba30b Reviewed-on: https://go-review.googlesource.com/c/tools/+/673796 Reviewed-by: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
40 lines
931 B
Go
40 lines
931 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.
|
|
if res, err := session.CallTool(ctx, "greet", map[string]any{"name": "you"}, nil); err != nil {
|
|
log.Printf("CallTool failed: %v", err)
|
|
} else {
|
|
if res.IsError {
|
|
log.Print("tool failed")
|
|
}
|
|
for _, c := range res.Content {
|
|
log.Print(c.Text)
|
|
}
|
|
}
|
|
}
|
|
|
|
//!-
|