Make Tool APIs future-proof by pushing generics into CallToolParams, and using CallToolParams throughout. This has several advantages: - We no longer need the awkward CallToolOptions type, which is almost but not quite like CallToolParams. - NewTool no longer needs to define its own handler type: it accepts a ToolHandler[TArgs]. This also means its signature is future-proof. Additionally, provide a `CallTool` helper (the analog of NewTool) to serve as a more strongly typed variant of `ClientSession.CallTool`. + design, protocol generation Change-Id: I80172a25bc50734dae84a541aa4776ed9161694e Reviewed-on: https://go-review.googlesource.com/c/tools/+/676495 Reviewed-by: Jonathan Amsterdam <jba@google.com> Auto-Submit: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
91 lines
2.4 KiB
Go
91 lines
2.4 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 main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
)
|
|
|
|
var httpAddr = flag.String("http", "", "if set, use SSE HTTP at this address, instead of stdin/stdout")
|
|
|
|
type HiArgs struct {
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
func SayHi(ctx context.Context, ss *mcp.ServerSession, params *mcp.CallToolParams[HiArgs]) (*mcp.CallToolResult, error) {
|
|
return &mcp.CallToolResult{
|
|
Content: []*mcp.Content{
|
|
mcp.NewTextContent("Hi " + params.Name),
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func PromptHi(ctx context.Context, ss *mcp.ServerSession, args *HiArgs, params *mcp.GetPromptParams) (*mcp.GetPromptResult, error) {
|
|
return &mcp.GetPromptResult{
|
|
Description: "Code review prompt",
|
|
Messages: []*mcp.PromptMessage{
|
|
{Role: "user", Content: mcp.NewTextContent("Say hi to " + params.Name)},
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
|
|
server := mcp.NewServer("greeter", "v0.0.1", nil)
|
|
server.AddTools(mcp.NewTool("greet", "say hi", SayHi, mcp.Input(
|
|
mcp.Property("name", mcp.Description("the name to say hi to")),
|
|
)))
|
|
server.AddPrompts(mcp.NewPrompt("greet", "", PromptHi))
|
|
server.AddResources(&mcp.ServerResource{
|
|
Resource: &mcp.Resource{
|
|
Name: "info",
|
|
MIMEType: "text/plain",
|
|
URI: "embedded:info",
|
|
},
|
|
Handler: handleEmbeddedResource,
|
|
})
|
|
|
|
if *httpAddr != "" {
|
|
handler := mcp.NewSSEHandler(func(*http.Request) *mcp.Server {
|
|
return server
|
|
})
|
|
http.ListenAndServe(*httpAddr, handler)
|
|
} else {
|
|
t := mcp.NewLoggingTransport(mcp.NewStdIOTransport(), os.Stderr)
|
|
if err := server.Run(context.Background(), t); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Server failed: %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
var embeddedResources = map[string]string{
|
|
"info": "This is the hello example server.",
|
|
}
|
|
|
|
func handleEmbeddedResource(_ context.Context, _ *mcp.ServerSession, params *mcp.ReadResourceParams) (*mcp.ReadResourceResult, error) {
|
|
u, err := url.Parse(params.URI)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if u.Scheme != "embedded" {
|
|
return nil, fmt.Errorf("wrong scheme: %q", u.Scheme)
|
|
}
|
|
key := u.Opaque
|
|
text, ok := embeddedResources[key]
|
|
if !ok {
|
|
return nil, fmt.Errorf("no embedded resource named %q", key)
|
|
}
|
|
return &mcp.ReadResourceResult{
|
|
Contents: []*mcp.ResourceContents{mcp.NewTextResourceContents(params.URI, "text/plain", text)},
|
|
}, nil
|
|
}
|