internal/mcp: export the Transport interface; add a Server example
Export the Transport interface, as there's no reason users shouldn't be able to define their own transports: they are just bidirectional streams. Also, add a godoc example for basic Server usage. Finally, remove some TODOs that existed due to a bad merge: they are related to the (as yet unsubmitted) streamable HTTP transport. Change-Id: Id669793fc80630e667d50611fe2876d09125a025 Reviewed-on: https://go-review.googlesource.com/c/tools/+/668675 TryBot-Bypass: Robert Findley <rfindley@google.com> Reviewed-by: Sam Thanawalla <samthanawalla@google.com> Auto-Submit: Robert Findley <rfindley@google.com>
This commit is contained in:
parent
fd3ce0d810
commit
3b86fb5601
1 changed files with 66 additions and 25 deletions
53
mcp/example_test.go
Normal file
53
mcp/example_test.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
// 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"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
)
|
||||
|
||||
type SayHiParams struct {
|
||||
Name string `json:"name" mcp:"the name to say hi to"`
|
||||
}
|
||||
|
||||
func SayHi(ctx context.Context, cc *mcp.ClientConnection, params *SayHiParams) ([]mcp.Content, error) {
|
||||
return []mcp.Content{
|
||||
mcp.TextContent{Text: "Hi " + params.Name},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func ExampleServer() {
|
||||
ctx := context.Background()
|
||||
clientTransport, serverTransport := mcp.NewLocalTransport()
|
||||
|
||||
server := mcp.NewServer("greeter", "v0.0.1", nil)
|
||||
server.AddTools(mcp.MakeTool("greet", "say hi", SayHi))
|
||||
|
||||
clientConnection, err := server.Connect(ctx, serverTransport, nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
client := mcp.NewClient("client", "v0.0.1", nil)
|
||||
serverConnection, err := client.Connect(ctx, clientTransport, nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
content, err := serverConnection.CallTool(ctx, "greet", SayHiParams{Name: "user"})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(content[0].(mcp.TextContent).Text)
|
||||
|
||||
serverConnection.Close()
|
||||
clientConnection.Wait()
|
||||
|
||||
// Output: Hi user
|
||||
}
|
||||
Loading…
Reference in a new issue