Fix an example using params.Name instead of params.Arguments.Name. All credit due to @A11Might for noticing this. Sending a separate PR to keep the internal/readme source consistent.
39 lines
1 KiB
Go
39 lines
1 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"
|
|
"log"
|
|
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
)
|
|
|
|
type HiParams struct {
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
func SayHi(ctx context.Context, cc *mcp.ServerSession, params *mcp.CallToolParamsFor[HiParams]) (*mcp.CallToolResultFor[any], error) {
|
|
return &mcp.CallToolResultFor[any]{
|
|
Content: []mcp.Content{&mcp.TextContent{Text: "Hi " + params.Arguments.Name}},
|
|
}, nil
|
|
}
|
|
|
|
func main() {
|
|
// Create a server with a single tool.
|
|
server := mcp.NewServer("greeter", "v1.0.0", nil)
|
|
server.AddTools(
|
|
mcp.NewServerTool("greet", "say hi", SayHi, mcp.Input(
|
|
mcp.Property("name", mcp.Description("the name of the person to greet")),
|
|
)),
|
|
)
|
|
// Run the server over stdin/stdout, until the client disconnects
|
|
if err := server.Run(context.Background(), mcp.NewStdioTransport()); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// !-
|