## Summary
This PR adds schema caching that dramatically reduces the cost of tool
registration in stateless server patterns.
**Contributors:** Thanks to @omgitsads and @almaleksia for collaborating
on and researching this solution!
## Problem
In stateless deployments like `github/github-mcp-server`, a new
`*mcp.Server` is created for each incoming request. This means `AddTool`
is called repeatedly for the same tools, causing:
1. **For typed handlers**: `jsonschema.ForType()` reflection is called
every time
2. **For pre-defined schemas**: `schema.Resolve()` is called every time
## Solution
Add a `schemaCache` that stores:
- Schemas by `reflect.Type` (for auto-generated schemas from typed
handlers)
- Resolved schemas by schema pointer (for pre-defined schemas)
The cache is:
- Concurrent-safe using `sync.Map`
- Unbounded (typical MCP servers have <100 tools)
- Global (lives across server instances)
## Benchmark Results
```
BenchmarkAddToolTypedHandler-22 977080 1223 ns/op 1208 B/op 21 allocs/op
BenchmarkAddToolTypedHandlerNoCache-22 6764 161463 ns/op 39262 B/op 1072 allocs/op
```
| Metric | With Cache | Without Cache | Improvement |
|--------|------------|---------------|-------------|
| Time | 1,223 ns/op | 161,463 ns/op | **132x faster** |
| Allocations | 21 allocs | 1,072 allocs | **51x fewer** |
| Memory | 1,208 B/op | 39,262 B/op | **32x less** |
## Files Changed
- `mcp/schema_cache.go` - New cache implementation
- `mcp/server.go` - Modified `setSchema` to use cache
- `mcp/schema_cache_test.go` - Unit tests for caching behavior
- `mcp/schema_cache_benchmark_test.go` - Benchmarks
## Impact for Integrators
**Automatic** - no code changes required. Integrators using:
- Typed handlers (`AddTool[In, Out]`) → cache by type
- Pre-defined schemas (`Tool{InputSchema: schema}`) → cache by pointer
Both patterns benefit from caching after the first call.
---
## Real-World Performance Validation
The following benchmarks were conducted using `github/github-mcp-server`
(a production MCP server with ~130 tools) to validate the performance
impact in a real-world scenario.
### Test Environment
- **Server:** github-mcp-server (stateless HTTP deployment)
- **Tools registered per request:** ~130 tools
- **Test methodology:** Python benchmark client, 30 iterations
(latency), 100 iterations (stress)
- **Date:** December 4, 2025
### Configurations Tested
| Configuration | Description |
|--------------|-------------|
| **main (mcp-go)** | Original implementation using mcp-go library |
| **go-sdk (no cache)** | go-sdk WITHOUT schema caching (broken) |
| **go-sdk (with cache)** | go-sdk WITH this PR's schema caching fix
|
### Latency Test Results (n=30)
#### Operation: `initialize`
| Configuration | P50 | P99 | Status |
|--------------|-----|-----|--------|
| main (mcp-go) | 11.48ms | 14.00ms | ✅ Baseline |
| go-sdk (no cache) | 20.47ms | 25.30ms | 🔴 **+78% REGRESSION** |
| go-sdk (with cache) | 10.36ms | 14.33ms | ✅ **FIXED (-10%)** |
#### Operation: `tools/list`
| Configuration | P50 | P99 | Status |
|--------------|-----|-----|--------|
| main (mcp-go) | 13.46ms | 22.47ms | ✅ Baseline |
| go-sdk (no cache) | 22.98ms | 29.11ms | 🔴 **+71% REGRESSION** |
| go-sdk (with cache) | 14.15ms | 15.91ms | ✅ **FIXED (+5%)** |
#### Operation: `prompts/list`
| Configuration | P50 | P99 | Status |
|--------------|-----|-----|--------|
| main (mcp-go) | 11.56ms | 13.81ms | ✅ Baseline |
| go-sdk (no cache) | 21.00ms | 26.42ms | 🔴 **+82% REGRESSION** |
| go-sdk (with cache) | 10.77ms | 15.25ms | ✅ **FIXED (-7%)** |
### Stress Test Results (n=100)
| Configuration | P50 (initialize) | P50 (tools/list) | P50
(prompts/list) |
|--------------|------------------|------------------|-------------------|
| main (mcp-go) | 12.06ms | 14.30ms | 11.34ms |
| go-sdk (no cache) | 20.06ms | 23.44ms | 19.59ms |
| go-sdk (with cache) | **11.83ms** | **14.58ms** | **11.05ms** |
### Memory/Allocation Comparison (from pprof)
| Configuration | Total Allocations | Comparison |
|--------------|-------------------|------------|
| main (mcp-go) | 355.91 MB | Baseline |
| go-sdk (no cache) | **1208.70 MB** | 🔴 **3.4x MORE allocations** |
#### Top Allocation Sources - go-sdk WITHOUT cache (broken)
| Function | Size | % | Issue |
|----------|------|---|-------|
| `jsonschema.UnmarshalJSON` | 324.60 MB | 27% | 🚨 Schema re-parsing |
| `encoding/json.Unmarshal` | 341.10 MB | 28% | JSON deserialization |
| `jsonschema.resolve` | 219.51 MB | 18% | 🚨 Schema re-resolution |
| `jsonschema.MarshalJSON` | 92.52 MB | 8% | Schema JSON encoding |
### Root Cause Analysis
The `google/jsonschema-go` library regenerates JSON schemas on every
request instead of caching them. In a server with ~130 tools, this
causes:
- **70-80% latency regression** on all MCP operations
- **3.4x more memory allocations** per request
- **~70% of all allocations** are schema-related operations
### Key Findings
1. ✅ **REGRESSION CONFIRMED:** go-sdk without schema caching is 70-80%
slower than mcp-go
2. ✅ **FIX VERIFIED:** Schema caching restores performance to baseline
(or better)
3. ✅ **MEMORY IMPACT:** 3.4x reduction in allocations with caching
4. ✅ **PRODUCTION READY:** Fixed version performs equivalently to mcp-go
baseline
---------
Co-authored-by: Adam Holt <4619+omgitsads@users.noreply.github.com>
Co-authored-by: Ksenia Bobrova <1885174+almaleksia@users.noreply.github.com>
336 lines
15 KiB
Markdown
336 lines
15 KiB
Markdown
# Support for MCP server features
|
|
|
|
%toc
|
|
|
|
## Prompts
|
|
|
|
MCP servers can provide LLM prompt templates (called simply
|
|
[_prompts_](https://modelcontextprotocol.io/specification/2025-06-18/server/prompts))
|
|
to clients. Every prompt has a required name which identifies it, and a set of
|
|
named arguments, which are strings.
|
|
|
|
**Client-side**: To list the server's prompts, use the
|
|
[`ClientSession.Prompts`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.Prompts)
|
|
iterator, or the lower-level
|
|
[`ClientSession.ListPrompts`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.ListPrompts)
|
|
(see [pagination](#pagination) below). Set
|
|
[`ClientOptions.PromptListChangedHandler`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientOptions.PromptListChangedHandler)
|
|
to be notified of changes in the list of prompts.
|
|
|
|
Call
|
|
[`ClientSession.GetPrompt`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.GetPrompt)
|
|
to retrieve a prompt by name, providing arguments for expansion.
|
|
|
|
**Server-side**: Use
|
|
[`Server.AddPrompt`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#Server.AddPrompt)
|
|
to add a prompt to the server along with its handler.
|
|
The server will have the `prompts` capability if any prompt is added before the
|
|
server is connected to a client, or if
|
|
[`ServerOptions.HasPrompts`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerOptions.HasPrompts)
|
|
is explicitly set. When a prompt is added, any clients already connected to the
|
|
server will be notified via a `notifications/prompts/list_changed`
|
|
notification.
|
|
|
|
%include ../../mcp/server_example_test.go prompts -
|
|
|
|
## Resources
|
|
|
|
In MCP terms, a _resource_ is some data referenced by a URI.
|
|
MCP servers can serve resources to clients.
|
|
They can register resources individually, or register a _resource template_
|
|
that uses a URI pattern to describe a collection of resources.
|
|
|
|
|
|
**Client-side**:
|
|
Call [`ClientSession.ReadResource`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.ReadResource)
|
|
to read a resource.
|
|
The SDK ensures that a read succeeds only if the URI matches a registered resource exactly,
|
|
or matches the URI pattern of a resource template.
|
|
|
|
To list a server's resources and resource templates, use the
|
|
[`ClientSession.Resources`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.Resources)
|
|
and
|
|
[`ClientSession.ResourceTemplates`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.ResourceTemplates)
|
|
iterators, or the lower-level `ListXXX` calls (see [pagination](#pagination)).
|
|
Set
|
|
[`ClientOptions.ResourceListChangedHandler`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientOptions.ResourceListChangedHandler)
|
|
to be notified of changes in the lists of resources or resource templates.
|
|
|
|
Clients can be notified when the contents of a resource changes by subscribing to the resource's URI.
|
|
Call
|
|
[`ClientSession.Subscribe`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.Subscribe)
|
|
to subscribe to a resource
|
|
and
|
|
[`ClientSession.Unsubscribe`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.Unsubscribe)
|
|
to unsubscribe.
|
|
Set
|
|
[`ClientOptions.ResourceUpdatedHandler`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientOptions.ResourceUpdatedHandler)
|
|
to be notified of changes to subscribed resources.
|
|
|
|
**Server-side**:
|
|
Use
|
|
[`Server.AddResource`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#Server.AddResource)
|
|
or
|
|
[`Server.AddResourceTemplate`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#Server.AddResourceTemplate)
|
|
to add a resource or resource template to the server along with its handler.
|
|
A
|
|
[`ResourceHandler`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ResourceHandler)
|
|
maps a URI to the contents of a resource, which can include text, binary data,
|
|
or both.
|
|
If `AddResource` or `AddResourceTemplate` is called before a server is connected, the server will have the
|
|
`resources` capability.
|
|
The server will have the `resources` capability if any resource or resource template is added before the
|
|
server is connected to a client, or if
|
|
[`ServerOptions.HasResources`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerOptions.HasResources)
|
|
is explicitly set. When a prompt is added, any clients already connected to the
|
|
server will be notified via a `notifications/resources/list_changed`
|
|
notification.
|
|
|
|
|
|
%include ../../mcp/server_example_test.go resources -
|
|
|
|
## Tools
|
|
|
|
MCP servers can provide
|
|
[tools](https://modelcontextprotocol.io/specification/2025-06-18/server/tools)
|
|
to allow clients to interact with external systems or functionality. Tools are
|
|
effectively remote function calls, and the Go SDK provides mechanisms to bind
|
|
them to ordinary Go functions.
|
|
|
|
**Client-side**: To list the server's tools, use the
|
|
[`ClientSession.Tools`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.Tools)
|
|
iterator, or the lower-level
|
|
[`ClientSession.ListTools`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.ListTools)
|
|
(see [pagination](#pagination)). Set
|
|
[`ClientOptions.ToolListChangedHandler`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientOptions.ToolListChangedHandler)
|
|
to be notified of changes in the list of tools.
|
|
|
|
To call a tool, use
|
|
[`ClientSession.CallTool`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.CallTool)
|
|
with `CallToolParams` holding the name and arguments of the tool to call.
|
|
|
|
```go
|
|
res, err := session.CallTool(ctx, &mcp.CallToolParams{
|
|
Name: "my_tool",
|
|
Arguments: map[string]any{"name": "user"},
|
|
})
|
|
```
|
|
|
|
Arguments may be any value that can be marshaled to JSON.
|
|
|
|
**Server-side**: the basic API for adding a tool is symmetrical with the API
|
|
for prompts or resources:
|
|
[`Server.AddTool`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#Server.AddTool)
|
|
adds a
|
|
[`Tool`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#Tool) to
|
|
the server along with its
|
|
[`ToolHandler`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ToolHandler)
|
|
to handle it. The server will have the `tools` capability if any tool is added
|
|
before the server is connected to a client, or if
|
|
[`ServerOptions.HasTools`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerOptions.HasPrompts)
|
|
is explicitly set. When a tool is added, any clients already connected to the
|
|
server will be notified via a `notifications/tools/list_changed` notification.
|
|
|
|
However, the `Server.AddTool` API leaves it to the user to implement the tool
|
|
handler correctly according to the spec, providing very little out of the box.
|
|
In order to implement a tool, the user must do all of the following:
|
|
|
|
- Provide a tool input and output schema.
|
|
- Validate the tool arguments against its input schema.
|
|
- Unmarshal the input schema into a Go value
|
|
- Execute the tool logic.
|
|
- Marshal the tool's structured output (if any) to JSON, and store it in the
|
|
result's `StructuredOutput` field as well as the unstructured `Content` field.
|
|
- Validate that output JSON against the tool's output schema.
|
|
- If any tool errors occurred, pack them into the unstructured content and set
|
|
`IsError` to `true.`
|
|
|
|
For this reason, the SDK provides a generic
|
|
[`AddTool`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#AddTool)
|
|
function that handles this for you. It can bind a tool to any function with the
|
|
following shape:
|
|
|
|
```go
|
|
func(_ context.Context, request *CallToolRequest, input In) (result *CallToolResult, output Out, _ error)
|
|
```
|
|
|
|
This is like a `ToolHandler`, but with an extra arbitrary `In` input parameter,
|
|
and `Out` output parameter.
|
|
|
|
Such a function can then be bound to the server using `AddTool`:
|
|
|
|
```go
|
|
mcp.AddTool(server, &mcp.Tool{Name: "my_tool"}, handler)
|
|
```
|
|
|
|
This does the following automatically:
|
|
|
|
- If `Tool.InputSchema` or `Tool.OutputSchema` are unset, the input and output
|
|
schemas are inferred from the `In` type, which must be a struct or map.
|
|
Optional `jsonschema` struct tags provide argument descriptions.
|
|
- Tool arguments are validated against the input schema.
|
|
- Tool arguments are marshaled into the `In` value.
|
|
- Tool output (the `Out` value) is marshaled into the result's
|
|
`StructuredOutput`, as well as the unstructured `Content`.
|
|
- Output is validated against the tool's output schema.
|
|
- If an ordinary error is returned, it is stored int the `CallToolResult` and
|
|
`IsError` is set to `true`.
|
|
|
|
In fact, under ordinary circumstances, the user can ignore `CallToolRequest`
|
|
and `CallToolResult`.
|
|
|
|
For a more realistic example, consider a tool that retrieves the weather:
|
|
|
|
%include ../../mcp/tool_example_test.go weathertool -
|
|
|
|
In this case, we want to customize part of the inferred schema, though we can
|
|
still infer the rest. Since we want to control the inference ourselves, we set
|
|
the `Tool.InputSchema` explicitly:
|
|
|
|
%include ../../mcp/tool_example_test.go customschemas -
|
|
|
|
_See [mcp/tool_example_test.go](../mcp/tool_example_test.go) for the full
|
|
example, or [examples/server/toolschemas](examples/server/toolschemas/main.go)
|
|
for more examples of customizing tool schemas._
|
|
|
|
**Stateless server deployments:** Some deployments create a new
|
|
[`Server`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#Server)
|
|
for each incoming request, re-registering tools every time. To avoid repeated
|
|
schema generation, create a
|
|
[`SchemaCache`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#SchemaCache)
|
|
and share it across server instances:
|
|
|
|
```go
|
|
var schemaCache = mcp.NewSchemaCache() // create once at startup
|
|
|
|
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
|
s := mcp.NewServer(impl, &mcp.ServerOptions{SchemaCache: schemaCache})
|
|
mcp.AddTool(s, myTool, myHandler)
|
|
// ...
|
|
}
|
|
```
|
|
|
|
## Utilities
|
|
|
|
### Completion
|
|
|
|
To support the
|
|
[completion](https://modelcontextprotocol.io/specification/2025-06-18/server/utilities/completion)
|
|
capability, the server needs a completion handler.
|
|
|
|
**Client-side**: completion is called using the
|
|
[`ClientSession.Complete`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.Complete)
|
|
method.
|
|
|
|
**Server-side**: completion is enabled by setting
|
|
[`ServerOptions.CompletionHandler`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerOptions.CompletionHandler).
|
|
If this field is set to a non-nil value, the server will advertise the
|
|
`completions` server capability, and use this handler to respond to completion
|
|
requests.
|
|
|
|
%include ../../examples/server/completion/main.go completionhandler -
|
|
|
|
### Logging
|
|
|
|
MCP servers can send logging messages to MCP clients.
|
|
(This form of logging is distinct from server-side logging, where the
|
|
server produces logs that remain server-side, for use by server maintainers.)
|
|
|
|
**Server-side**:
|
|
The minimum log level is part of the server state.
|
|
For stateful sessions, there is no default log level: no log messages will be sent
|
|
until the client calls `SetLevel` (see below).
|
|
For stateful sessions, the level defaults to "info".
|
|
|
|
[`ServerSession.Log`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerSession.Log) is the low-level way for servers to log to clients.
|
|
It sends a logging notification to the client if the level of the message
|
|
is at least the minimum log level.
|
|
|
|
For a simpler API, use [`NewLoggingHandler`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#NewLoggingHandler) to obtain a [`slog.Handler`](https://pkg.go.dev/log/slog#Handler).
|
|
By setting [`LoggingHandlerOptions.MinInterval`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#LoggingHandlerOptions.MinInterval), the handler can be rate-limited
|
|
to avoid spamming clients with too many messages.
|
|
|
|
Servers always report the logging capability.
|
|
|
|
|
|
**Client-side**:
|
|
Set [`ClientOptions.LoggingMessageHandler`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientOptions.LoggingMessageHandler) to receive log messages.
|
|
|
|
Call [`ClientSession.SetLevel`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.SetLevel) to change the log level for a session.
|
|
|
|
%include ../../mcp/server_example_test.go logging -
|
|
|
|
## Capabilities
|
|
|
|
Server capabilities are advertised to clients during the initialization
|
|
handshake. By default, the SDK advertises only the `logging` capability.
|
|
Additional capabilities are automatically added when features are registered
|
|
(e.g., adding a tool adds the `tools` capability).
|
|
|
|
### Capability inference
|
|
|
|
When features such as tools, prompts, or resources are added to the server
|
|
(e.g., via `Server.AddTool`), their capability is automatically inferred, with
|
|
default value `{listChanged:true}`. Similarly, if the
|
|
`ServerOptions.SubscribeHandler` or `ServerOptions.CompletionHandler` are set,
|
|
the corresponding capability is added.
|
|
|
|
### Explicit capabilities
|
|
|
|
To explicitly declare capabilities, or to override the [default inferred
|
|
capability](#capability-inference), set
|
|
[`ServerOptions.Capabilities`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerOptions.Capabilities).
|
|
This sets the default server capabilities, before any capabilities are added
|
|
based on configured handlers. If a capability is already present as a field in
|
|
`Capabilities`, adding a feature or handler will not change its configuration.
|
|
|
|
This allows you to:
|
|
|
|
- **Disable default capabilities**: Pass an empty `&ServerCapabilities{}` to
|
|
disable all defaults, including logging.
|
|
- **Disable listChanged notifications**: Set `ListChanged: false` on a
|
|
capability to prevent the server from sending list-changed notifications
|
|
when features are added or removed.
|
|
- **Pre-declare capabilities**: Declare capabilities before features are
|
|
registered, useful for servers that load features dynamically.
|
|
|
|
```go
|
|
// Disable listChanged notifications for tools
|
|
server := mcp.NewServer(impl, &mcp.ServerOptions{
|
|
Capabilities: &mcp.ServerCapabilities{
|
|
Logging: &mcp.LoggingCapabilities{},
|
|
Tools: &mcp.ToolCapabilities{ListChanged: false},
|
|
},
|
|
})
|
|
```
|
|
|
|
**Deprecated**: The `HasPrompts`, `HasResources`, and `HasTools` fields on
|
|
`ServerOptions` are deprecated. Use `Capabilities` instead.
|
|
|
|
### Pagination
|
|
|
|
Server-side feature lists may be
|
|
[paginated](https://modelcontextprotocol.io/specification/2025-06-18/server/utilities/pagination),
|
|
using cursors. The SDK supports this by default.
|
|
|
|
**Client-side**: The `ClientSession` provides methods returning
|
|
[iterators](https://go.dev/blog/range-functions) for each feature type.
|
|
These iterators are an `iter.Seq2[Feature, error]`, where the error value
|
|
indicates whether page retrieval failed.
|
|
|
|
- [`ClientSession.Prompts`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.Prompts)
|
|
iterates prompts.
|
|
- [`ClientSession.Resource`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.Resource)
|
|
iterates resources.
|
|
- [`ClientSession.ResourceTemplates`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.ResourceTemplates)
|
|
iterates resource templates.
|
|
- [`ClientSession.Tools`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.Tools)
|
|
iterates tools.
|
|
|
|
The `ClientSession` also exposes `ListXXX` methods for fine-grained control
|
|
over pagination.
|
|
|
|
**Server-side**: pagination is on by default, so in general nothing is required
|
|
server-side. However, you may use
|
|
[`ServerOptions.PageSize`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerOptions.PageSize)
|
|
to customize the page size.
|