all: add examples of customizing tool schemas, and clarify documentation (#421)
Add example/server/toolschemas, which demonstrates various ways to customize tool schemas, and illustrates the differences between ToolHandler and ToolHandlerFor. Fixes #385 Fixes #386 For #368
This commit is contained in:
parent
5ccd97fafc
commit
40b6bd30f1
1 changed files with 309 additions and 100 deletions
|
|
@ -12,20 +12,21 @@ import (
|
|||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
)
|
||||
|
||||
type HiParams struct {
|
||||
type Input struct {
|
||||
Name string `json:"name" jsonschema:"the name of the person to greet"`
|
||||
}
|
||||
|
||||
func SayHi(ctx context.Context, req *mcp.CallToolRequest, args HiParams) (*mcp.CallToolResult, any, error) {
|
||||
return &mcp.CallToolResult{
|
||||
Content: []mcp.Content{&mcp.TextContent{Text: "Hi " + args.Name}},
|
||||
}, nil, nil
|
||||
type Output struct {
|
||||
Greeting string `json:"greeting" jsonschema:"the greeting to tell to the user"`
|
||||
}
|
||||
|
||||
func SayHi(ctx context.Context, req *mcp.CallToolRequest, input Input) (*mcp.CallToolResult, Output, error) {
|
||||
return nil, Output{Greeting: "Hi " + input.Name}, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Create a server with a single tool.
|
||||
server := mcp.NewServer(&mcp.Implementation{Name: "greeter", Version: "v1.0.0"}, nil)
|
||||
|
||||
mcp.AddTool(server, &mcp.Tool{Name: "greet", Description: "say hi"}, SayHi)
|
||||
// Run the server over stdin/stdout, until the client disconnects
|
||||
if err := server.Run(context.Background(), &mcp.StdioTransport{}); err != nil {
|
||||
|
|
|
|||
Loading…
Reference in a new issue