* mcp: move to 2025-06-18 schema Add types and fields for the latest MCP schema. The main challenge here was dealing with the presence of _meta fields on many types, like Tool, Prompt and Resource. - Distinguish between the _meta field of a request's params, which includes a progress token, from the _meta field on other types, which is simply a map. - Redefine GetMeta to return a pointer to the map. We wanted to avoid that, but I don't see a clean way to do so. An alternative would be to have GetMeta return a map, and SetMeta to set it. - Provide GetProgressToken for requests only (not notifications or other types). - Still to do: deal with the meta and annotation fields of ContentBlock, which are currently duplicated for Resource and ResourceContents. * Meta and progress token redesign
74 lines
1.9 KiB
Go
74 lines
1.9 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"`
|
|
}
|
|
|
|
func SayHi(ctx context.Context, cc *mcp.ServerSession, params *mcp.CallToolParamsFor[SayHiParams]) (*mcp.CallToolResultFor[any], error) {
|
|
return &mcp.CallToolResultFor[any]{
|
|
Content: []*mcp.ContentBlock{
|
|
mcp.NewTextContent("Hi " + params.Arguments.Name),
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func ExampleServer() {
|
|
ctx := context.Background()
|
|
clientTransport, serverTransport := mcp.NewInMemoryTransports()
|
|
|
|
server := mcp.NewServer("greeter", "v0.0.1", nil)
|
|
server.AddTools(mcp.NewServerTool("greet", "say hi", SayHi))
|
|
|
|
serverSession, err := server.Connect(ctx, serverTransport)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
client := mcp.NewClient("client", "v0.0.1", nil)
|
|
clientSession, err := client.Connect(ctx, clientTransport)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
res, err := clientSession.CallTool(ctx, &mcp.CallToolParams{
|
|
Name: "greet",
|
|
Arguments: map[string]any{"name": "user"},
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Println(res.Content[0].Text)
|
|
|
|
clientSession.Close()
|
|
serverSession.Wait()
|
|
|
|
// Output: Hi user
|
|
}
|
|
|
|
// createSessions creates and connects an in-memory client and server session for testing purposes.
|
|
func createSessions(ctx context.Context) (*mcp.ClientSession, *mcp.ServerSession, *mcp.Server) {
|
|
server := mcp.NewServer("server", "v0.0.1", nil)
|
|
client := mcp.NewClient("client", "v0.0.1", nil)
|
|
serverTransport, clientTransport := mcp.NewInMemoryTransports()
|
|
serverSession, err := server.Connect(ctx, serverTransport)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
clientSession, err := client.Connect(ctx, clientTransport)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
return clientSession, serverSession, server
|
|
}
|