Add support for 'prompts/list' and 'prompts/get', and registering prompts on the server using AddPrompts. Add a 'MakePrompt' helper to construct prompts from a prompt handler using reflection. Change-Id: I479cb9c9b99313cca99640c1f5ea1939363759e6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/669015 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Jonathan Amsterdam <jba@google.com>
50 lines
1.2 KiB
Go
50 lines
1.2 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"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
)
|
|
|
|
type AddParams struct {
|
|
X, Y int
|
|
}
|
|
|
|
func Add(ctx context.Context, cc *mcp.ClientConnection, params *AddParams) ([]mcp.Content, error) {
|
|
return []mcp.Content{
|
|
mcp.TextContent{Text: fmt.Sprintf("%d", params.X+params.Y)},
|
|
}, nil
|
|
}
|
|
|
|
func ExampleSSEHandler() {
|
|
server := mcp.NewServer("adder", "v0.0.1", nil)
|
|
server.AddTools(mcp.MakeTool("add", "add two numbers", Add))
|
|
|
|
handler := mcp.NewSSEHandler(func(*http.Request) *mcp.Server { return server })
|
|
httpServer := httptest.NewServer(handler)
|
|
defer httpServer.Close()
|
|
|
|
ctx := context.Background()
|
|
transport := mcp.NewSSEClientTransport(httpServer.URL)
|
|
serverConn, err := mcp.NewClient("test", "v1.0.0", nil).Connect(ctx, transport, nil)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer serverConn.Close()
|
|
|
|
content, err := serverConn.CallTool(ctx, "add", map[string]any{"x": 1, "y": 2})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Println(content[0].(mcp.TextContent).Text)
|
|
|
|
// Output: 3
|
|
}
|