- Go 99.7%
- Shell 0.3%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
Make Tool APIs future-proof by pushing generics into CallToolParams, and using CallToolParams throughout. This has several advantages: - We no longer need the awkward CallToolOptions type, which is almost but not quite like CallToolParams. - NewTool no longer needs to define its own handler type: it accepts a ToolHandler[TArgs]. This also means its signature is future-proof. Additionally, provide a `CallTool` helper (the analog of NewTool) to serve as a more strongly typed variant of `ClientSession.CallTool`. + design, protocol generation Change-Id: I80172a25bc50734dae84a541aa4776ed9161694e Reviewed-on: https://go-review.googlesource.com/c/tools/+/676495 Reviewed-by: Jonathan Amsterdam <jba@google.com> Auto-Submit: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> |
||
| design | ||
| examples | ||
| internal | ||
| jsonschema | ||
| mcp | ||
| CONTRIBUTING.md | ||
| README.md | ||
MCP SDK prototype
Contents
The mcp package provides a software development kit (SDK) for writing clients and servers of the model context protocol. It is unstable, and will change in breaking ways in the future. As of writing, it is a prototype to explore the design space of client/server transport and binding.
Installation
The mcp package is currently internal and cannot be imported using go get.
Quickstart
Here's an example that creates a client that talks to an MCP server running as a sidecar process:
package main
import (
"context"
"log"
"os/exec"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
func main() {
ctx := context.Background()
// Create a new client, with no features.
client := mcp.NewClient("mcp-client", "v1.0.0", nil)
// Connect to a server over stdin/stdout
transport := mcp.NewCommandTransport(exec.Command("myserver"))
session, err := client.Connect(ctx, transport)
if err != nil {
log.Fatal(err)
}
defer session.Close()
// Call a tool on the server.
params := &mcp.CallToolParams[map[string]any]{
Name: "greet",
Arguments: map[string]any{"name": "you"},
}
if res, err := mcp.CallTool(ctx, session, params); err != nil {
log.Printf("CallTool failed: %v", err)
} else {
if res.IsError {
log.Print("tool failed")
}
for _, c := range res.Content {
log.Print(c.Text)
}
}
}
Here is an example of the corresponding server, connected over stdin/stdout:
package main
import (
"context"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
type HiParams struct {
Name string `json:"name"`
}
func SayHi(ctx context.Context, cc *mcp.ServerSession, params *mcp.CallToolParams[HiParams]) (*mcp.CallToolResult, error) {
return &mcp.CallToolResult{
Content: []*mcp.Content{mcp.NewTextContent("Hi " + params.Name)},
}, nil
}
func main() {
// Create a server with a single tool.
server := mcp.NewServer("greeter", "v1.0.0", nil)
server.AddTools(mcp.NewTool("greet", "say hi", SayHi))
// Run the server over stdin/stdout, until the client disconnects
_ = server.Run(context.Background(), mcp.NewStdIOTransport())
}
Design
See design.md for the SDK design. That document is canonical: given any divergence between the design doc and this prototype, the doc reflects the latest design.
Testing
To test your client or server using stdio transport, you can use an in-memory transport. See example.
To test your client or server using sse transport, you can use the httptest package. See example.
Code of Conduct
This project follows the Go Community Code of Conduct. If you encounter a conduct-related issue, please mail conduct@golang.org.
License
Unless otherwise noted, the Go source files are distributed under the BSD-style license found in the LICENSE file.
Upon a potential move to the modelcontextprotocol organization, the license will be updated to the MIT License, and the license header will reflect the Go MCP SDK Authors.