Documentation
¶
Overview ¶
Package connectmcp serves the RPCs of a connect-go v2 server as MCP tools.
It is a peer of connecthttp: where connecthttp serves a *connect.Server over the Connect, gRPC and gRPC-Web protocols, connectmcp serves it over the Model Context Protocol. It works at runtime from the method descriptors of the registered services and needs no generated code of its own.
A method becomes a tool when it carries the (connectmcp.v1.tool_description) option, or when WithTool names it for a proto you do not own; other methods are not exposed:
rpc GetItem(GetItemRequest) returns (GetItemResponse) {
option (connectmcp.v1.tool_description) = "Look up one item by its ID.";
}
The tool's JSON schemas are derived from the request and response messages, and its name from the method's full name or the (connectmcp.v1.tool_name) option.
server := connect.NewServer(interceptors...)
itemsv1connect.RegisterItemServiceHandler(server, &itemService{})
mux := http.NewServeMux()
connecthttp.Mount(mux, server) // Connect, gRPC, gRPC-Web
handler, err := connectmcp.NewHandler(server) // MCP
mux.Handle("/mcp", handler)
Every tool call is dispatched with connect.Server.Call, so the server's interceptors run for tool calls exactly as they do for RPCs.
Index ¶
- Constants
- func NewHandler(server *connect.Server, opts ...Option) (http.Handler, error)
- func Register(s mcpserver.MCPServer, server *connect.Server, opts ...Option) error
- type FieldOptions
- type Option
- func WithField(field string, options FieldOptions) Option
- func WithReadMaxBytes(n int) Option
- func WithServerInfo(name, version string) Option
- func WithStreamableHTTPOptions(options *mcp.StreamableHTTPOptions) Option
- func WithTool(procedure string, tool ToolOptions) Option
- func WithToolAnnotations(annotate func(toolName string, method protoreflect.MethodDescriptor, ...)) Option
- func WithToolFilter(keep func(toolName string) bool) Option
- type ToolOptions
- type Transport
Constants ¶
const DefaultReadMaxBytes = 4 * 1024 * 1024 // 4 MiB
DefaultReadMaxBytes is the default limit on the size of a tool call's arguments, matching connecthttp's default limit on a request message.
const EnvelopeMaxBytes = 64 * 1024 // 64 KiB
EnvelopeMaxBytes is the room NewHandler gives an HTTP request body on top of the WithReadMaxBytes limit, for the JSON-RPC envelope around a tool call's arguments: the method, the request ID, the tool name and the request metadata (_meta), which is a few hundred bytes from a typical client.
const Protocol = "mcp"
Protocol is the CallInfo.Protocol value of RPCs that arrive as MCP tool calls.
Variables ¶
This section is empty.
Functions ¶
func NewHandler ¶
NewHandler returns an http.Handler that serves the tools of server over MCP's streamable HTTP transport.
The handler reads at most the WithReadMaxBytes limit plus EnvelopeMaxBytes from a request body, by default 4 MiB + 64 KiB.
The headers of the HTTP request carrying a tool call are the request headers of the RPC, so header-based authentication written for connect keeps working.
To serve MCP over another transport (such as stdio), or to add prompts and resources, create the MCP server yourself and use Register.
See Register for the conditions under which an error is returned.
func Register ¶
Register adds a tool to s for every method registered on server that carries the (connectmcp.v1.tool_description) option. Methods without the option, and methods whose schema is not a protobuf method descriptor, are skipped. For the official MCP go-sdk, wrap the *mcp.Server with gosdk.Wrap.
Each tool is annotated from the method's idempotency_level and (connectmcp.v1.tool_annotations) options (see toolschema.ToolAnnotationsForMethod), so clients can tell a read from a delete; WithToolAnnotations adjusts that.
Register reads the methods registered on server at the time of the call, so register the services first. It returns an error, without adding any tool, if:
- no registered method carries the option;
- the option is set to an empty description;
- the option is set on a streaming method;
- an (connectmcp.v1.tool_name) option is set to an invalid name;
- an (connectmcp.v1.tool_annotations) option contradicts the idempotency_level option (destructive_hint true on a NO_SIDE_EFFECTS method);
- a request or response message has a (buf.validate) rule this binary cannot decode (see toolschema.CheckValidateRules);
- two tools end up with the same name;
- a WithTool procedure is not registered, or a WithField field is in no tool's request or response, or has the (connectmcp.v1.field_description) option already while WithField sets a description.
Types ¶
type FieldOptions ¶ added in v0.0.8
type FieldOptions = toolschema.FieldOptions
FieldOptions are the Go form of a field's (connectmcp.v1.field_description) option and REQUIRED field behavior, for WithField.
type Option ¶
type Option func(*config)
Option configures NewHandler and Register.
func WithField ¶ added in v0.0.8
func WithField(field string, options FieldOptions) Option
WithField gives the field with the full name field, such as "google.longrunning.GetOperationRequest.name", the description and REQUIRED field behavior in options, for fields whose protos you do not own. Like the field_description option, the description shows wherever a tool's schema has the field. Register fails if no tool's request or response has the field, or if options sets a description on a field that has the (connectmcp.v1.field_description) option already.
func WithReadMaxBytes ¶
WithReadMaxBytes limits the size of the request message a tool call can carry: the JSON of its arguments, in bytes. It is the counterpart of connecthttp.WithReadMaxBytes and defaults to DefaultReadMaxBytes; zero allows any size.
The arguments are checked before they are decoded. Larger arguments fail the call when the handler receives its request message, with the same RESOURCE_EXHAUSTED error connecthttp returns ("message is larger than configured max N"), so interceptors see it. The client gets that error as a tool error even when an interceptor rewrites it.
NewHandler also bounds what it reads from an HTTP request with this limit: a request body may be n bytes plus EnvelopeMaxBytes for the JSON-RPC envelope around the arguments, and zero allows a body of any size. A larger body is not read to its end and is answered with HTTP 413, which no interceptor sees. To choose the body limit yourself, set MaxRequestBodyBytes in WithStreamableHTTPOptions.
Register cannot bound the body, which is read by the transport of the MCP server the caller owns. Note that go-sdk's streamable HTTP handler has a body limit of its own (4 MiB by default) that arguments must also fit in.
func WithServerInfo ¶
WithServerInfo sets the name and version the MCP server created by NewHandler reports to clients. The default is "connectmcp" and "0.0.0". It has no effect on Register, where the caller owns the MCP server.
func WithStreamableHTTPOptions ¶
func WithStreamableHTTPOptions(options *mcp.StreamableHTTPOptions) Option
WithStreamableHTTPOptions sets the options of the streamable HTTP transport created by NewHandler, for example to serve statelessly or with plain JSON responses. It has no effect on Register.
A zero MaxRequestBodyBytes is derived from WithReadMaxBytes rather than being go-sdk's default; set it to override that.
func WithTool ¶ added in v0.0.8
func WithTool(procedure string, tool ToolOptions) Option
WithTool exposes the method of procedure as a tool, as if it carried the (connectmcp.v1.*) options in tool, for methods whose protos you do not own, such as google.longrunning.Operations. What tool sets takes precedence over the method's own options, and is checked like them. When the method does not declare an idempotency_level, the IdempotencyLevel of its connect.Spec stands in for it. Register fails if no method of procedure is registered.
func WithToolAnnotations ¶
func WithToolAnnotations(annotate func(toolName string, method protoreflect.MethodDescriptor, annotations *mcpserver.ToolAnnotations)) Option
WithToolAnnotations lets code adjust the annotations of each tool after they are derived from the method's idempotency_level and (connectmcp.v1.tool_annotations) options. annotate is called once per registered tool (after WithToolFilter) with those annotations, never nil, and may change them in place: to set a hint for every tool, such as OpenWorldHint false for a service that only touches its own data, or to annotate a forwarded service whose protos you do not own. What annotate sets is not checked against idempotency_level.
func WithToolFilter ¶
WithToolFilter registers only the annotated tools for which keep returns true, given the tool name. Useful to expose a conditional subset of the tools (e.g. feature-gated writes).
type ToolOptions ¶ added in v0.0.8
type ToolOptions struct {
// Name is the tool name; empty derives it from the method's full name.
Name string
// Description is what the model reads about the tool; it must not be empty.
Description string
// Annotations are the title and hints; nil sets none.
Annotations *connectmcpv1.ToolAnnotations
}
ToolOptions are the Go form of the (connectmcp.v1.tool_name), (connectmcp.v1.tool_description) and (connectmcp.v1.tool_annotations) method options, for WithTool.
type Transport ¶ added in v0.0.6
type Transport struct {
// contains filtered or unexported fields
}
Transport is a connect.Transport that calls RPCs as MCP tools on a server served with NewHandler (or Register). It is the client-side counterpart of NewHandler, as connecthttp.NewTransport is of connecthttp.Mount: a generated client built on it reaches the server through MCP, so tests written against the client can cover the MCP surface too.
transport := connectmcp.NewTransport(http.DefaultClient, "https://api.example.com/mcp") defer transport.Close() client := itemsv1connect.NewItemServiceClient(connect.NewClient(transport))
Create it with NewTransport. It is safe for concurrent use.
func NewTransport ¶ added in v0.0.6
NewTransport returns a Transport that calls the tools of the MCP server at url over streamable HTTP, using httpClient (http.DefaultClient when nil).
The MCP session is opened by the first RPC and ended by Close. Each RPC is one tools/call request:
- The tool is the one NewHandler names for the method (toolschema.ToolNameForMethod), given the same WithTool options; NewTransport ignores the other options.
- The request message is sent as the tool arguments in the shape of the tool's input schema (toolcodec.EncodeMessage), without validating it.
- The request headers set on the call (connect.NewClientContext) are added to the HTTP request carrying it, so they are the request headers the server's interceptors see. Headers the MCP client sets itself, such as Content-Type, are kept.
- A tool error is returned as the *connect.Error it describes, built only from the code, message and details that arrived (toolcodec.ParseError).
MCP carries no response headers or trailers, so those of the call stay empty. Only unary RPCs can be called; others fail with CodeUnimplemented.
func (*Transport) NewClientStream ¶ added in v0.0.6
func (t *Transport) NewClientStream(ctx context.Context, spec connect.Spec) (connect.ClientStream, error)
NewClientStream implements connect.Transport.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
Package main serves a connect-go v2 service as MCP tools over stdio.
|
Package main serves a connect-go v2 service as MCP tools over stdio. |
|
connect
command
Package main serves a connect-go v2 server over HTTP twice: as a regular connect API, and as MCP tools under /mcp.
|
Package main serves a connect-go v2 server over HTTP twice: as a regular connect API, and as MCP tools under /mcp. |
|
internal
|
|
|
mcptest
Package mcptest provides an in-memory MCP server for tests, backed by the official go-sdk, that is driven with raw JSON-RPC request messages.
|
Package mcptest provides an in-memory MCP server for tests, backed by the official go-sdk, that is driven with raw JSON-RPC request messages. |
|
pkg
|
|
|
mcpserver
Package mcpserver is the MCP server surface that tools are registered on, independent of any MCP library.
|
Package mcpserver is the MCP server surface that tools are registered on, independent of any MCP library. |
|
mcpserver/gosdk
Package gosdk provides an adapter from modelcontextprotocol/go-sdk to the mcpserver.MCPServer interface.
|
Package gosdk provides an adapter from modelcontextprotocol/go-sdk to the mcpserver.MCPServer interface. |
|
toolcodec
Package toolcodec holds the encodings between protobuf messages, connect errors and the JSON of MCP tool calls: arguments, results and tool errors, each in both directions.
|
Package toolcodec holds the encodings between protobuf messages, connect errors and the JSON of MCP tool calls: arguments, results and tool errors, each in both directions. |
|
toolschema
Package toolschema derives MCP tool definitions and their JSON schemas from protobuf descriptors.
|
Package toolschema derives MCP tool definitions and their JSON schemas from protobuf descriptors. |