This CL contains an minimal prototype of an MCP SDK, using the "new" jsonrpc2_v2 package (similar to x/exp/jsonrpc2). Much is still yet to do, but this initial version addressed the following aspects: - Support for newline delimited JSONRPC2 framing, and message logging. - A generated protocol package, containing type definitions to be used in transport. - A minimal jsonschema package to be used for both reading the MCP spec, and for serving Tool input schemas. - A transport abstraction, and two transport implementations: stdio, and local (for testing). - Client and Server types, to be configured with features and then connected using their Connect methods, which return (respectively) a ServerConnection and ClientConnection object. - A minimal binding API for tools. A catalog of things not yet done is in doc.go, but we should review this CL eagerly, as it contains the fundamental building blocks of an eventual SDK. It is the intention that this package is more-or-less standalone, as it may eventually be carved out into a separate repository. As such, its only dependency within x/tools is jsonrpc2_v2, and the only change required to that package is to expose a callback when the connection is closed. Change-Id: Ib66018961014f85a8884d56ab721d5642c98443c Reviewed-on: https://go-review.googlesource.com/c/tools/+/667036 Reviewed-by: Jonathan Amsterdam <jba@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
70 lines
2.1 KiB
Go
70 lines
2.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 mcp
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"github.com/modelcontextprotocol/go-sdk/internal/jsonschema"
|
|
"github.com/modelcontextprotocol/go-sdk/internal/protocol"
|
|
)
|
|
|
|
// A ToolHandler handles a call to tools/call.
|
|
type ToolHandler func(context.Context, json.RawMessage) (*protocol.CallToolResult, error)
|
|
|
|
// A Tool is a tool definition that is bound to a tool handler.
|
|
type Tool struct {
|
|
Definition protocol.Tool
|
|
Handler ToolHandler
|
|
}
|
|
|
|
// MakeTool is a helper to make a tool using reflection on the given handler.
|
|
//
|
|
// The input schema for the tool is extracted from the request type for the
|
|
// handler, and used to unmmarshal and validate requests to the handler.
|
|
//
|
|
// It is the caller's responsibility that the handler request type can produce
|
|
// a valid schema, as documented by [jsonschema.ForType]; otherwise, MakeTool
|
|
// panics.
|
|
func MakeTool[TReq any](name, description string, handler func(context.Context, TReq) ([]Content, error)) *Tool {
|
|
schema, err := jsonschema.ForType[TReq]()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
wrapped := func(ctx context.Context, args json.RawMessage) (*protocol.CallToolResult, error) {
|
|
var v TReq
|
|
if err := unmarshalSchema(args, schema, &v); err != nil {
|
|
return nil, err
|
|
}
|
|
content, err := handler(ctx, v)
|
|
if err != nil {
|
|
return &protocol.CallToolResult{
|
|
Content: marshalContent([]Content{TextContent{Text: err.Error()}}),
|
|
IsError: true,
|
|
}, nil
|
|
}
|
|
res := &protocol.CallToolResult{
|
|
Content: marshalContent(content),
|
|
}
|
|
return res, nil
|
|
}
|
|
return &Tool{
|
|
Definition: protocol.Tool{
|
|
Name: name,
|
|
Description: description,
|
|
InputSchema: schema,
|
|
},
|
|
Handler: wrapped,
|
|
}
|
|
}
|
|
|
|
// unmarshalSchema unmarshals data into v and validates the result according to
|
|
// the given schema.
|
|
func unmarshalSchema(data json.RawMessage, _ *jsonschema.Schema, v any) error {
|
|
// TODO: use reflection to create the struct type to unmarshal into.
|
|
// Separate validation from assignment.
|
|
return json.Unmarshal(data, v)
|
|
}
|