Add more documentation for the mcp package, explaining how to create and use Clients and Servers. Also add an example of using the SSEHandler to crete an MCP server. Change-Id: Ib327826b2cdb1e3418482551c65569ec6f01af33 Reviewed-on: https://go-review.googlesource.com/c/tools/+/668715 Reviewed-by: Jonathan Amsterdam <jba@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Neal Patel <nealpatel@google.com> Auto-Submit: Robert Findley <rfindley@google.com>
53 lines
1.2 KiB
Go
53 lines
1.2 KiB
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 mcp_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
)
|
|
|
|
type SayHiParams struct {
|
|
Name string `json:"name" mcp:"the name to say hi to"`
|
|
}
|
|
|
|
func SayHi(ctx context.Context, cc *mcp.ClientConnection, params *SayHiParams) ([]mcp.Content, error) {
|
|
return []mcp.Content{
|
|
mcp.TextContent{Text: "Hi " + params.Name},
|
|
}, nil
|
|
}
|
|
|
|
func ExampleServer() {
|
|
ctx := context.Background()
|
|
clientTransport, serverTransport := mcp.NewLocalTransport()
|
|
|
|
server := mcp.NewServer("greeter", "v0.0.1", nil)
|
|
server.AddTools(mcp.MakeTool("greet", "say hi", SayHi))
|
|
|
|
clientConnection, err := server.Connect(ctx, serverTransport, nil)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
client := mcp.NewClient("client", "v0.0.1", nil)
|
|
serverConnection, err := client.Connect(ctx, clientTransport, nil)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
content, err := serverConnection.CallTool(ctx, "greet", SayHiParams{Name: "user"})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Println(content[0].(mcp.TextContent).Text)
|
|
|
|
serverConnection.Close()
|
|
clientConnection.Wait()
|
|
|
|
// Output: Hi user
|
|
}
|