connectmcp

package module
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 17 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

connectmcp-go

[!WARNING] This library is meant for internal use. Do not rely on its stability: it may change or break without notice.

[!WARNING] The code was written with the help of AI coding tools. It has been reviewed by a human.

Test Go Report Card

connectmcp-go lets AI assistants use an API you already have. If your API is built with ConnectRPC in Go, this library offers it as a Model Context Protocol (MCP) server, the standard way for assistants like Claude and ChatGPT to call tools.

You mark the operations an assistant may use and describe them in your API definition. The library does the rest: each marked operation becomes a tool, with the same authentication, validation and behavior as the rest of your API. There is no second implementation to write or keep in sync.

It sits next to the other ways of serving a Connect API: connecthttp for Connect and gRPC, and vanguard-go for REST.

[!NOTE] This project started as a fork of redpanda-data/protoc-gen-go-mcp and has since become something quite different. Upstream is a protoc plugin that generates MCP code; this library generates nothing and works from a running connect-go v2 server instead, which is also why it was renamed. See Changes from upstream for the differences.

Changes from upstream

Forked from upstream at 6334690.

  • No protoc plugin. Tools are built at runtime from the descriptors of a *connect.Server (connectmcp.NewHandler / connectmcp.Register); no *.pb.mcp.go files are generated.
  • Opt-in tools. Only methods annotated with (connectmcp.v1.tool_description) are exposed; misconfiguration (empty description, streaming method, invalid or duplicate name) is a registration error.
  • connect-go v2 only. Tool calls go through connect.Server.Call, so interceptors run and HTTP headers become request headers. gRPC and the mark3labs/mcp-go adapter are removed; only the official go-sdk is supported.
  • Correctable argument errors. Undecodable arguments are returned as an INVALID_ARGUMENT tool error, in the same JSON shape as any other failed call, naming the offending field (also as a google.rpc.BadRequest detail) and the expected shape, so the model can retry.
  • Tool annotations. Tools announce MCP's readOnlyHint and idempotentHint from the standard idempotency_level option, and a title, destructiveHint and openWorldHint from (connectmcp.v1.tool_annotations), so clients can tell a read from a delete.
  • Validation rules in the schema. Every protovalidate rule reaches the tool's JSON schema, found by reflection instead of a fixed list: standard rules, the rules of list items and map keys and values, predefined (custom) rules, CEL rules and example values. A rule is a JSON Schema keyword where one exists and a line in the property's description otherwise (upstream: string length, pattern, uuid/email and some numeric bounds; anything else was dropped silently).
  • Field descriptions. (connectmcp.v1.field_description) describes a field to the model, as the description of its property in the tool schemas, so field guidance no longer has to be written into the tool description.
  • Request size limit. connectmcp.WithReadMaxBytes limits the size of a tool call's arguments like connecthttp.WithReadMaxBytes limits a request message (default 4 MiB); larger arguments are a RESOURCE_EXHAUSTED tool error. NewHandler bounds the HTTP request body with it too.
  • Removed: descriptor-only gen.RegisterService, extra properties, the tool name prefix and runtime.Option (the filter is now connectmcp.WithToolFilter).
  • Module and proto. The Go module is github.com/advdv/connectmcp-go, with the connectmcp package at its root. The annotations are connectmcp/v1/annotations.proto in proto package connectmcp.v1 (upstream: mcp.v1, same extension numbers), with Go code in pkg/connectmcpv1, published to buf.build/advdv/connectmcp on each release.
  • Tooling. Tools pinned with mise, reproducible just generate, golangci-lint in CI, GitHub-hosted runners.

Features

  • Serves a connect-go v2 server as MCP in one call, through its interceptors
  • Tools are selected and described with a proto option, and so are their fields
  • Tool annotations (read-only, idempotent, destructive, title) from idempotency_level and a proto option
  • JSON Schema for tool inputs and outputs derived from the request and response messages
  • protovalidate rules and example values in those schemas, custom and CEL rules included
  • Tool calls can be forwarded to a remote connect server
  • Built on modelcontextprotocol/go-sdk (the official SDK)

Usage

Annotate the RPCs to expose

A method becomes a tool when it carries the (connectmcp.v1.tool_description) option. Methods without it are not exposed.

import "connectmcp/v1/annotations.proto";

service ItemService {
  rpc GetItem(GetItemRequest) returns (GetItemResponse) {
    option (connectmcp.v1.tool_description) = "Look up one item by its ID.";
  }
}

The description is what the model sees. It is a proto option rather than the RPC's comment because the descriptors compiled into a Go binary carry options but not comments. See Importing the annotation with Buf for how to depend on the annotation.

Generate the standard *.pb.go and connect-go v2 code as usual; nothing else is generated.

Describe the fields

A field is described to the model with the (connectmcp.v1.field_description) option, for the same reason: comments do not reach the binary.

message ListItemsRequest {
  int32 page_size = 1 [
    (connectmcp.v1.field_description) = "How many items to return. The server returns at most 100, and 20 when unset.",
    (buf.validate.field).int32.gte = 0
  ];
}

The text is the description of the field's property in the input and output schema of every tool that uses the message:

"page_size": {
  "type": "integer",
  "minimum": 0,
  "description": "How many items to return. The server returns at most 100, and 20 when unset."
}

Say what the field means and what the server does with it, such as defaults and limits it applies itself. Validation rules and example values are added to the schema from the buf.validate options, so they need not be repeated in the text or in the tool description. On a message field the text describes the message as used in that field. Fields without the option have no description.

Serve MCP
import (
    "connectrpc.com/connect/v2"
    "connectrpc.com/connect/v2/connecthttp"
    "github.com/advdv/connectmcp-go"
)

server := connect.NewServer(validate.NewServerInterceptor())
itemsv1connect.RegisterItemServiceHandler(server, &srv)

mcpHandler, err := connectmcp.NewHandler(server)

mux := http.NewServeMux()
connecthttp.Mount(mux, server)  // the connect API
mux.Handle("/mcp", mcpHandler)  // the annotated RPCs as MCP tools
  • Every tool call is dispatched with server.Call, so the server's interceptors (auth, validation, tracing) run for tool calls exactly as they do for RPCs.
  • The headers of the HTTP request carrying a tool call are the RPC's request headers (CallInfo.RequestHeader()), and CallInfo.Protocol is "mcp".
  • NewHandler reads the methods registered at the time of the call, so register the services first.
  • NewHandler returns an error if no method is annotated, a description is empty, an annotated method is streaming, a configured tool name is invalid, a tool annotation contradicts idempotency_level, a message has a validation rule that cannot be decoded, or two tools share a name.

Options:

connectmcp.NewHandler(server,
    connectmcp.WithServerInfo("items", "1.0.0"),
    connectmcp.WithStreamableHTTPOptions(&mcp.StreamableHTTPOptions{Stateless: true, JSONResponse: true}),
)

See examples/connect for a runnable version.

Other transports, prompts and resources

NewHandler is a convenience around connectmcp.Register, which adds the tools to an MCP server you own. Use it to serve over stdio or to add prompts and resources. It programs against the one-method mcpserver.MCPServer interface; the gosdk package adapts the official go-sdk server to it:

// raw is *mcp.Server for transport, s is mcpserver.MCPServer for tools.
raw, s := gosdk.NewServer("my-server", "1.0.0")

err := connectmcp.Register(s, server)

raw.Run(ctx, &mcp.StdioTransport{})

See examples/basic.

Forwarding to a remote connect server

In connect-go v2 a generated client has the same method signatures as a service handler, so a client can be registered as the handler. Tool calls are then forwarded:

client := itemsv1connect.NewItemServiceClient(connect.NewClient(
    connecthttp.NewTransport(http.DefaultClient, "https://api.example.com"),
))

server := connect.NewServer()
itemsv1connect.RegisterItemServiceHandler(server, client)

The upstream's errors are remote to this server, so, as over connect-go's own transports, the model sees them as a bare INTERNAL (see Errors). To pass their code, message and details on, re-author them as the server's own with an interceptor:

func trustUpstreamErrors(next connect.ServerFunc) connect.ServerFunc {
    return func(ctx context.Context, spec connect.Spec, stream connect.ServerStream) error {
        err := next(ctx, spec, stream)
        if cerr, ok := errors.AsType[*connect.Error](err); ok && cerr != nil && cerr.IsRemote() {
            local := connect.NewError(cerr.Code(), cerr.Message()).WithCause(err)
            for _, detail := range cerr.Details() {
                local = local.WithDetail(detail)
            }
            return local
        }
        return err
    }
}

server := connect.NewServer(trustUpstreamErrors)
Errors

An error returned from the RPC is given to the model as a tool error (isError: true) containing JSON. It reveals exactly what connect-go's own transports put on the wire for that error, so serving a server as MCP discloses nothing it would not disclose over Connect, gRPC or gRPC-Web. A *connect.Error created by the handler (anywhere in the error chain) supplies the code, its own message and the details:

{
  "code": "NOT_FOUND",
  "message": "no such item",
  "details": [{"@type": "type.googleapis.com/google.rpc.ResourceInfo", "resourceType": "item", "resourceName": "item-1"}]
}

Everything else is reported by code alone, without a message:

  • text added by wrapping a connect error (fmt.Errorf("...: %w", cerr)) and its .WithCause(err) cause are not shown;
  • a remote *connect.Error (decoded from a peer, e.g. returned by a connect client) is INTERNAL;
  • context.Canceled is CANCELED, and context.DeadlineExceeded (or os.ErrDeadlineExceeded) is DEADLINE_EXCEEDED;
  • any other error is UNKNOWN.

So return connect.NewError(...) with a message meant for the model, and keep internals in the wrapped error or in .WithCause(err) for logging.

Calling the tools with a generated client

connectmcp.NewTransport is the client-side counterpart of NewHandler: a connect.Transport that calls each RPC as an MCP tool over streamable HTTP. A generated client built on it reaches the server through MCP, so tests written against the client can run once per way of reaching the server:

for name, transport := range map[string]connect.Transport{
    "connect": connecthttp.NewTransport(srv.Client(), srv.URL+"/rpc"),
    "mcp":     connectmcp.NewTransport(srv.Client(), srv.URL+"/mcp"), // has Close, to end the MCP session
} {
    client := itemsv1connect.NewItemServiceClient(connect.NewClient(transport))
    // the same assertions on responses, *connect.Error codes and details
}

The request message is sent as tool arguments in the shape of the tool's input schema, without validating it. A tool error comes back as a remote *connect.Error built only from the code, message and details that arrived. Request headers set on a call (connect.NewClientContext) are sent with the HTTP request of that call. MCP has no response headers or trailers, and only unary RPCs can be called.

To do the same over an MCP session you own, toolcodec.EncodeMessage, toolcodec.DecodeResult and toolcodec.ParseError are the encodings the transport uses.

Request size limit

connectmcp.WithReadMaxBytes(n) is the counterpart of connecthttp.WithReadMaxBytes: it limits the request message, measured as the JSON of the tool call's arguments, to n bytes. The default is 4 MiB, as in connecthttp, and zero allows any size. The limit is checked before the arguments are decoded. Larger arguments fail the call, while the handler receives its request message, with connecthttp's own error, and interceptors see it:

{"code": "RESOURCE_EXHAUSTED", "message": "message is larger than configured max 1048576"}

NewHandler bounds the HTTP request body with the same limit: a body may be n bytes plus connectmcp.EnvelopeMaxBytes (64 KiB) for the JSON-RPC envelope around the arguments, so by default it reads at most 4 MiB + 64 KiB per request, and zero allows a body of any size. A larger body is not read to its end and is answered with HTTP 413 before any tool or interceptor is called; connectmcp.NewTransport returns that as RESOURCE_EXHAUSTED too. Arguments over n in a body that fits are always the tool error above. To choose the body limit yourself, set MaxRequestBodyBytes in WithStreamableHTTPOptions. With Register the body is read by the transport of your own MCP server: go-sdk's streamable HTTP handler has its own body limit (MaxRequestBodyBytes, 4 MiB by default), which the arguments must fit in as well.

Using grpc-go

grpc-go is not supported: tools are served from a connect-go v2 *connect.Server.

Tool filtering

connectmcp.WithToolFilter registers only the annotated tools for which it returns true, for example to gate writes behind a feature flag.:

connectmcp.NewHandler(server,
    connectmcp.WithToolFilter(func(toolName string) bool { return !writes[toolName] }),
)
Tool annotations

MCP tools carry annotations: a display title and hints that clients use to decide, for example, which calls to run without asking the user. A tool without hints is assumed to be possibly destructive and to reach into an open world, so annotate every tool.

Whether a method has side effects is declared with the standard idempotency_level option, the same declaration Connect uses for GET requests (and AIP for REST). connectmcp derives the hints from it:

idempotency_level readOnlyHint idempotentHint destructiveHint
NO_SIDE_EFFECTS true true false
IDEMPOTENT true
unset

The (connectmcp.v1.tool_annotations) option adds what idempotency_level cannot express: the title clients display instead of the tool name, whether a method with side effects is destructive (destructive_hint, false for one that only adds data), and whether it reaches outside the service's own domain (open_world_hint). A service that manages workspaces:

service WorkspaceService {
  rpc GetWorkspace(GetWorkspaceRequest) returns (Workspace) {
    option idempotency_level = NO_SIDE_EFFECTS;
    option (connectmcp.v1.tool_description) = "Look up one workspace by its ID.";
    option (connectmcp.v1.tool_annotations) = {title: "Get workspace", open_world_hint: false};
  }
  rpc CreateWorkspace(CreateWorkspaceRequest) returns (Workspace) {
    option (connectmcp.v1.tool_description) = "Create a new workspace.";
    option (connectmcp.v1.tool_annotations) = {title: "Create workspace", destructive_hint: false, open_world_hint: false};
  }
  rpc UpdateWorkspace(UpdateWorkspaceRequest) returns (Workspace) {
    option idempotency_level = IDEMPOTENT;
    option (connectmcp.v1.tool_description) = "Replace the fields of a workspace.";
    option (connectmcp.v1.tool_annotations) = {title: "Update workspace", open_world_hint: false};
  }
  rpc DeleteWorkspace(DeleteWorkspaceRequest) returns (DeleteWorkspaceResponse) {
    option idempotency_level = IDEMPOTENT;
    option (connectmcp.v1.tool_description) = "Delete a workspace and all of its data.";
    option (connectmcp.v1.tool_annotations) = {title: "Delete workspace", destructive_hint: true, open_world_hint: false};
  }
}

is listed as:

Tool title readOnlyHint idempotentHint destructiveHint openWorldHint
GetWorkspace Get workspace true true false false
CreateWorkspace Create workspace false false
UpdateWorkspace Update workspace true (default true) false
DeleteWorkspace Delete workspace true true false

The title is sent both as the tool's title and as annotations.title, for clients of older MCP versions. destructive_hint: true on a NO_SIDE_EFFECTS method is a contradiction, and NewHandler returns an error for it. The full example is tool_annotations.proto.

To set annotations in code, for example a default for every tool or for a forwarded service whose protos you do not own, use connectmcp.WithToolAnnotations. It is called for every registered tool with the annotations derived from the protos, which it may change. These changes are not checked against idempotency_level:

connectmcp.NewHandler(server,
    // Every tool works on this service's data only, unless its proto says otherwise.
    connectmcp.WithToolAnnotations(func(_ string, _ protoreflect.MethodDescriptor, a *mcpserver.ToolAnnotations) {
        if a.OpenWorldHint == nil {
            a.OpenWorldHint = proto.Bool(false)
        }
    }),
)

Annotations are hints for clients, not a security boundary: authorize tool calls in an interceptor.

Tool naming

By default a tool is named after the method's full proto name with dots replaced by underscores (redpanda_mcps_sql_v1_SQLService_Query). Names longer than 64 characters — the strictest provider limit — are truncated: a 10-character hash of the full name plus as much of the tail (the most specific part) as fits. The hash is forced to start with a letter because Gemini rejects function names that start with a digit.

To publish a method under a stable, human-chosen name instead, set the (connectmcp.v1.tool_name) method option:

import "connectmcp/v1/annotations.proto";

rpc GetEquityResearchReport(GetEquityResearchReportRequest) returns (GetEquityResearchReportResponse) {
  option (connectmcp.v1.tool_name) = "get_equity_research_report";
}

The value must match ^[a-z_][a-z0-9_-]{0,63}$ — lowercase snake/kebab case within every provider's function-name rules; lowercase guarantees configured names pass untouched through consumers' de-mangling heuristics (they key on the UpperCamel Service_ segment of derived names). connectmcp returns an error for an invalid value and for two tools with the same name.

Importing the annotation with Buf

Depend on the annotation through the Buf Schema Registry instead of vendoring it:

# buf.yaml
deps:
  - buf.build/advdv/connectmcp

In managed mode, keep its go_package so your generated *.pb.go code imports the connectmcpv1 package from this Go module. Don't generate Go code for it yourself, because a second copy conflicts at registration:

# buf.gen.yaml
managed:
  disable:
    - file_option: go_package
      module: buf.build/advdv/connectmcp

To match the module version in go.mod, pin the label for the same Git commit: buf.build/advdv/connectmcp:<git-sha>.

Development & Testing

Tooling

Developer tools (Go, just, bazelisk, buf, golangci-lint, gofumpt, gh) are pinned in mise.toml. Install them with mise:

mise install
Commands

All commands use just:

just build                # Build everything (Bazel)
just test-unit            # Unit tests (no API keys needed)
just test                 # All tests including conformance/integration (needs API keys)
just test-cover           # Tests with coverage report
just generate             # Regenerate the annotation and test proto code
just lint                 # Run golangci-lint
just fmt                  # Format code
just gazelle              # Sync BUILD files from go.mod

# View all available commands
just --list
Development Workflow
# 1. Edit protos or code
# 2. Regenerate
just generate
# 3. Run tests
just test-unit

Proto-to-JSON Schema mapping

Protobuf types are mapped to JSON Schema. Understanding these mappings matters because LLMs see the schema, not your proto definitions.

Scalar types
Proto type JSON Schema type Notes
double, float number
int32, uint32, sint32, fixed32, sfixed32 integer
int64, uint64, sint64, fixed64, sfixed64 string JSON cannot represent 64-bit integers precisely
bool boolean
string string
bytes string contentEncoding: base64
enum string enum array with all value names
Messages and maps

Nested messages become nested object schemas. map<K, V> becomes a JSON object whose values have the schema of V, with propertyNames constraining the keys (a pattern for integer keys, an enum for bool keys).

Well-known types
Proto type JSON Schema
google.protobuf.Timestamp string with format: date-time
google.protobuf.Duration string with a duration pattern
google.protobuf.Struct object with additionalProperties: true
google.protobuf.Value any (no type constraint)
google.protobuf.ListValue array
google.protobuf.FieldMask string
google.protobuf.Any object with @type + value
Wrapper types (StringValue, etc.) nullable scalar (e.g., ["string", "null"])
Oneof fields

A oneof is one property, named after the oneof: an object with a which discriminator that names the member that is set, and one property per member. Providers reject a union (anyOf/oneOf) at the top of a tool's input schema, which is why it is an object.

"item_type": {
  "type": "object",
  "description": "Exactly one of the \"item_type\" group. Set \"which\" to the chosen field name, then set only that field.",
  "required": ["which"],
  "properties": {
    "which": {"type": "string", "enum": ["product", "service"], "description": "Which field of the \"item_type\" oneof is set."},
    "product": {"type": "object", "description": "The value when which=\"product\".", "...": "..."},
    "service": {"type": "object", "description": "The value when which=\"service\".", "...": "..."}
  }
}

The arguments of a call are turned back into the oneof before they are decoded, and results are written in the same shape. A oneof with (buf.validate.oneof).required is in the parent's required.

Validation rules

protovalidate rules (buf.validate) and example values are part of the schema, so the model knows what a valid value looks like before it calls the tool. See examples/basic for a request with most kinds of rules and its schema.

The rules are found by reflection over the rule messages, not from a list of supported rules, so every rule that is set is in the schema: standard rules, the rules of list items and of map keys and values, predefined rules, and CEL rules. Each rule is in one place.

A JSON Schema keyword, when one accepts exactly what the rule accepts:

Rule JSON Schema
const const (64-bit integers as a string, bytes as base64, a field_mask as its string)
in enum
gte / lte minimum / maximum
gt / lt on an integer minimum + 1 / maximum - 1
gt / lt on a float or double exclusiveMinimum / exclusiveMaximum
string.len, min_len, max_len minLength / maxLength
string.pattern pattern
string.email, hostname, ipv4, ipv6, uri, uri_ref, uuid format
enum.const, in, not_in the listed enum names are narrowed to the allowed ones
repeated.min_items, max_items minItems / maxItems
repeated.items the same mapping, in items
map.min_pairs, max_pairs minProperties / maxProperties
map.keys, map.values the same mapping, in propertyNames / additionalProperties
example examples
(buf.validate.field).required the field is in the parent's required, like (google.api.field_behavior) = REQUIRED

A line in the description for every other rule, after Rules:. A standard rule is written as its name and value, with the value in its JSON form:

{"type": "string", "description": "Rules:\n- string.prefix = \"x-\"\n- string.not_in = [\"x-admin\"]"}
{"type": ["string", "null"], "format": "date-time", "description": "Rules: timestamp.gt_now"}

This is also where a rule goes when a keyword would be wrong: the bounds of a 64-bit integer or a numeric map key (JSON strings, which minimum does not apply to), and bounds that protovalidate reads as "outside the range" (gt: 10, lt: 5). repeated.unique is described too, because OpenAI rejects a tool schema with uniqueItems.

CEL rules are described by their message. A rule without one computes its message in the expression, so the expression is shown instead:

  • (buf.validate.field).cel and cel_expression are lines in the description of the field;
  • (buf.validate.message).cel, cel_expression and oneof are lines in the description of the message, which for the request itself is the description of the input schema;
  • a predefined rule, an extension of a rule message that you declare with (buf.validate.predefined), is its name and value followed by the messages of its CEL rules: uuid_name = "workspaces": value must be a resource name with a UUID in lower case as its ID.

Modifiers. With ignore = IGNORE_IF_ZERO_VALUE the keywords are kept and the description says that the rules are skipped when the value is unset or zero. With IGNORE_ALWAYS only the examples are kept.

Three things to know:

  • The schema informs, it does not enforce: tool arguments are not validated against it. Add a protovalidate interceptor to the connect server; its violations reach the model as an INVALID_ARGUMENT tool error.
  • Descriptions cost tokens in every tools/list. Rules with a keyword add no description, and a field without rules adds nothing.
  • A rule that the binary cannot decode is a registration error instead of a gap in the schema. That is a predefined rule whose Go package is not linked into the binary, or a rule newer than the protovalidate Go module (buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go) in your go.mod.

What a field means is not a rule: describe it with (connectmcp.v1.field_description), whose text comes before the rules in the description. Field comments are not in the schema, because the descriptors compiled into a Go binary do not carry them.

Recursive messages

Self-referencing and mutually recursive messages (e.g., tree nodes, linked lists) are supported. The schema expands up to 3 levels deep with full field detail. Beyond that, recursive fields become {"type": "string", "description": "JSON-encoded TreeNode. Provide a JSON object as a string."}.

Such a string is parsed back into an object before the arguments are decoded. The actual data can nest arbitrarily deep -- the depth limit only applies to the schema the LLM sees, not to what it can send.

Tool name mangling

If the fully qualified RPC name (dots replaced with underscores) exceeds 64 characters, the name is truncated: the head is replaced with a 10-character SHA-256 hash prefix, preserving the tail (the most specific part, typically ServiceName_MethodName). The 64-char limit exists because Claude desktop enforces it.

Limitations

  • Only unary RPCs are exposed as tools.
  • Recursive message schemas lose field-level detail beyond 3 levels of nesting (see above). The LLM must encode deeper levels as JSON strings.

Feedback

We'd love feedback, bug reports, or PRs! Join the discussion and help shape the future of Go and Protobuf MCP tooling.

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; methods without it 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

View Source
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.

View Source
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.

View Source
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

func NewHandler(server *connect.Server, opts ...Option) (http.Handler, error)

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

func Register(s mcpserver.MCPServer, server *connect.Server, opts ...Option) error

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.

Types

type Option

type Option func(*config)

Option configures NewHandler and Register.

func WithReadMaxBytes

func WithReadMaxBytes(n int) Option

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

func WithServerInfo(name, version string) Option

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 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

func WithToolFilter(keep func(toolName string) bool) Option

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 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

func NewTransport(httpClient *http.Client, url string) *Transport

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).
  • 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) Close added in v0.0.6

func (t *Transport) Close() error

Close ends the MCP session. RPCs fail after Close.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL