After some experience with the flattened version of Content, we see that it can easily lead to incorrect usage and is a harder API to read. Therefore, this CL changes back to a design similar to what we had prior to https://go.dev/cl/672415, though opting to promote content unmarshalling to the protocol types that use it, which leads to an overall simpler api.
35 lines
919 B
Go
35 lines
919 B
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.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))
|
|
// Run the server over stdin/stdout, until the client disconnects
|
|
if err := server.Run(context.Background(), mcp.NewStdioTransport()); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// !-
|