Commit graph go-sdk/mcp
Author SHA1 Message Date
Robert Findley
1c3f38f777
all: more minor cleanup (#547)
Fix broken / missing links.
2025-09-30 16:07:07 -04:00
Robert Findley
6dbf6c6450
all: minor cleanup (#545)
Also fix a flaky test case in TestStreamableServerTransport.
2025-09-30 15:53:00 -04:00
Chuck Ha
b835e07c26
mcp: fix inconsistent logging new lines (#541)
This change keeps the logging consistent with a new line after each log
whether an error or not.

Fixes #540

Co-authored-by: Chuck Ha <cha@nvidia.com>
2025-09-29 14:19:05 -04:00
Robert Findley
ab092510d2
mcp: fix inaccurate package doc (#534) 2025-09-26 14:16:10 -04:00
Robert Findley
b2048cd73a
mcp: unexport incidentally exported error codes (#536)
Unexport error codes that were accidentally exported. I don't think it's
possible that they could be useful for users (see #535).

Fixes #535
2025-09-26 14:04:28 -04:00
Robert Findley
964574b818
mcp: allow any value for schemas (#517)
As discussed at length in
https://github.com/modelcontextprotocol/go-sdk/issues/518, coupling our
schema fields to
jsonschema.Schema limits our user's ability to use a different schema
package, and makes it impossible for our client to talk to some
backends.

This CL relaxes the type of `*jsonschema.Schema` fields to be of type
`any`, which is a breaking change but is unlikely to affect most server
code. (It *is* a nontrivial change for clients).

Fixes https://github.com/modelcontextprotocol/go-sdk/issues/518
2025-09-26 11:55:54 -04:00
Jonathan Amsterdam
5f05f8a43a
many: minor cosmetic changes (#529) 2025-09-25 16:06:24 -04:00
Robert Findley
07f6c490c6
mcp: make the streamable client transport less strict (#524)
In practice, the streamable client transport was having trouble
connecting to various backends, because they are nonconformant in
various ways.

While it would be nice if all servers conformed to the spec, in practice
there are certain spec violations that are recoverable, and we can and
should recover them. Specifically:

- tolerate 404 instead of 405 for the hanging GET (#393)
- tolerate (=ignore) spurious response body for notifications and
responses, since we know none are expected

Additionally, fix a bug that we weren't parsing Content-Type correctly.

Fixes #521
2025-09-24 14:07:27 -04:00
Manuel Ibar
5d64d61974
mcp: fix context propagation in StreamableClientTransport (#514)
## Problem
Context propagation was broken in `StreamableClientTransport` because:
1. `Connect()` used `context.Background()` instead of the parent context
2. `Close()` created a race condition where DELETE requests were
cancelled before completion

## Solution  
- Use parent context when creating the connection context in `Connect()`
- Reorder `Close()` operations to perform cleanup DELETE before
cancelling context

## Impact
- Request-scoped values (auth tokens, trace IDs) now propagate correctly
to background HTTP operations
- Eliminates race condition in cleanup operations  
- Maintains proper Go context semantics

Fixes #513
2025-09-23 12:27:39 -04:00
Jonathan Amsterdam
33ff851ef3
mcp: update to jsonschema that does not validate structs (#511)
Update to the latest jsonschema commit, which removes the ability to
validate structs and changes the type of ForOptions.TypeSchemas.
2025-09-22 15:28:04 -04:00
Suraj Bobade
8c42b69817
SSE: Add support for SSE handler options (#508)
mcp/sse: add support to provide for options for SSE transport

- Add SSEOptions struct to define SSE handler options.
- Update Signature of NewSSEHandler to accept SSEOptions [braking
change]
- Update unit test
- Update example test

Fixes https://github.com/modelcontextprotocol/go-sdk/issues/507,
https://github.com/modelcontextprotocol/go-sdk/issues/503
2025-09-19 13:01:57 -04:00
Jonathan Amsterdam
4e9a387d04
doc: document logging (#497)
For #442.
2025-09-19 11:58:58 -04:00
Jonathan Amsterdam
4fa725857c
docs: document resources (#505) 2025-09-19 11:46:50 -04:00
Robert Findley
5985a7cbba
docs: document tool support (#498)
Document tool support, and tweak the wording for prompts.

For #442
2025-09-19 10:48:59 -04:00
Robert Findley
1696b595c6
mcp: fix several goroutine leaks in tests (#494)
Fix some (but not all) goroutine leaks in tests. Others were nontrivial,
because they relate to streamable http.

For #489.
2025-09-18 14:24:45 -04:00
Jonathan Amsterdam
fed5106631
mcp: add prompt feature doc (#487) 2025-09-18 12:27:02 -04:00
Friedrich Große
02f0b25286
Move GetSessionID closure into ServerOptions (#488)
Fixes: #478.
2025-09-18 12:21:02 -04:00
Robert Findley
353d46ff86
mcp: remove the StreamID type (#490)
The streamID type did not carry its weight. Remove it.

Fixes #484.
2025-09-18 12:01:07 -04:00
Robert Findley
b636b1633b
mcp: fix flaky ExampleLoggingTransport (#483)
Reads are technically racy with writes. Sort for stability.
2025-09-18 08:30:53 -04:00
Jonathan Amsterdam
140b93980c
mcp: add CallToolResult.getError (#481)
Provide a way for middleware to get the error from a tool call,
demonstrating that we can add this functionality without making a
breaking change.

Leave getError unexported for now; we can export it (and setError) at
any time.

For #64.
2025-09-18 08:08:23 -04:00
Robert Findley
b615fa49b1
mcp: add a streamable serving benchmark
Add a benchmark using the streamable HTTP handler, and a more realistic input/output signature. Also expose this handler as an example (both a go example and a server in the examples/ directory).
2025-09-17 17:49:31 -04:00
Rob Findley
a3e753b8d8 mcp: correct the JSON used for unstructured content
Recently, I refactored to fix validation and default application for
structured output. However, I used the wrong byte slice for the
unstructured output. Fix it to use the same bytes as structured output,
with a test.

Fixes #475
2025-09-17 17:42:43 -04:00
Rob Findley
bd6ffe3b4c mcp: add an example customizing a nested type schema
Add an example that demonstrates how to customize a nested type schema,
which is a recurring problem for our users (see #467).

Fixes #467
Updates #368
2025-09-16 21:34:27 -04:00
Rob Findley
845c29f6b7 mcp: add a benchmark for the MemoryEventStore; remove validation
Add a new benchmark measuring the append and purge performance of the
MemoryEventStore. This benchmark revealed that the store is orders of
magnitude slower than it should be due to conservative validation
(hugely so: 300KB/s vs 568MB/s). Turn off this validation by default.

For #190
2025-09-16 15:50:31 -04:00
Robert Findley
ac2f175214
docs: add feature and troubleshooting documentation (#463)
Add a framework for feature documentation, and start populating it with
our SDK documentation.

This framework is as follows:
- internal/docs/**.src.md is the markdown source for the docs/
  directory.
- The x/example/internal/cmd/weave tool is used to compile these docs to
  the top-level docs, supporting both linked code samples and generated
  tables of contents.
- The readme-check workflow is updated to check these docs as well.
- The structure of these docs follows the MCP spec.
- Wherever possible, example code is linked from actual Go documentation
  examples, so that it is testable.

Some minor modifications to the weave tool were made to support this
framework.

Additionally, partially fill out this documentation with content on
base protocol and client features, as well as troubleshooting help.

Along the way, a bug was encountered that our LoggingTransport was not
concurrency safe. This is fixed with a mutex.

Fixes https://github.com/modelcontextprotocol/go-sdk/issues/466
Fixes https://github.com/modelcontextprotocol/go-sdk/issues/409
Updates https://github.com/modelcontextprotocol/go-sdk/issues/442
2025-09-16 11:13:40 -04:00
Jonathan Amsterdam
ab4d6218c6 mcp: tweak ProgressNotificationParams 2025-09-16 10:37:47 -04:00
Rob Findley
159933cfa3 mcp: speed up TestCommandTransportDuration
At 7s, this test was 70% of our test execution time on my computer.
Speed it up by mutating the defaultTerminateDuration for the purposes of
the test.

Also update our test workflow to use go1.25, now that it's out.
2025-09-12 12:00:29 -04:00
Rob Findley
9f1b8646bd mcp: propertly validate against JSON, independent of Go values
Our validation logic was avoiding double-unmarshalling as much as
possible, by parsing before validation and validating the Go type.

This only works if the Go type has the same structure as its JSON
representation, which may not be the case in the presence of types with
custom MarshalJSON or UnmarshalJSON methods (such as time.Time).

But even if the Go type doesn't use any custom marshalling, validation
is broken, because we can't differentiate zero values from missing
values.

Bite the bullet and use double-unmarshalling for both input and output
schemas. Coincidentally, this fixes three bugs:
- We were accepting case-insensitive JSON keys, since we parsed first,
  even though they should have been rejected. A number of tests were
  wrong.
- Defaults were overriding present-yet-zero values, as noted in an
  incorrect test case.
- When "arguments" was missing, validation wasn't performed, no defaults
  were applied, and unmarshalling failed even if all properties were
  optional.

First unmarshalling to map[string]any allows us to fix all these bugs.
Unfortunately, it means a 3x increase in the number of reflection
operations (we need to unmarshal, apply defaults and validate,
re-marshal with the defaults, and then unmarshal into the Go type).
However, this is not likely to be a significant overhead, and we can
always optimize in the future.

Update github.com/google/jsonschema-go to pick up necessary improvements
supporting this change.

Additionally, fix the error codes for invalid tool parameters, to be
consistent with other SDKs (Invalid Params: -32602).

Fixes #447
Fixes #449
Updates #450
2025-09-12 10:50:19 -04:00
Sam Thanawalla
b66679350e
transport.go: disable stdio batching for newer protocols (#453)
This PR disables batching support for stdio by storing the negotiated
protocol version and comparing it to protocolVersion20250618.

Fixes: #21
2025-09-11 13:33:46 -04:00
ankitm123
bf3ff50a16 mcp: remove json-rpc batching for more recent protocol versions
JSON-RPC batching support is removed for protocol versions greater than or
equal to protocolVersion20250618

Fixes #21
2025-09-11 10:50:56 -04:00
Rob Findley
872b437b2a internal/readme: use go:generate rather than Make
Simplify the readme generation to just use go:generate, no longer
depending on Make or bash.

Subsequent CLs will generate more documentation in a similar manner.

For #442
2025-09-11 10:38:07 -04:00
Sam Thanawalla
3a66cf3997
go.mod: update jsonschema to v0.2.2 (#446)
This version of jsonschema fixes embedded structs.

Fixes: #437
2025-09-11 10:21:02 -04:00
Rob Findley
728e0e3ac9 mcp: improve error messages from Wait for streamable clients
Previously, the error message received from ClientSession.Wait would
only report the closeErr, which would often be nil even if the client
transport was broken. Wait should return the reason the session
terminated, if abnormal.

I'm not sure of the exact semantics of this, but surely returning nil is
less useful than returning a meaningful non-nil error. We can refine our
handling of errors once we have more feedback.

Also add a test for client termination on HTTP server shutdown,
described in #265. This should work as long as (1) the session is
stateful (with a hanging GET), or (2) the session is stateless but the
client has a keepalive ping.

Also: don't send DELETE if the session was terminated with 404; +test.

Fixes #265
2025-09-10 11:24:26 -04:00
Rob Findley
b1c75f04d8 mcp: remove deprecated transport constructors
In #272, we made transports open structs and deprecated their
constructors. However, we left the constructors in place to allow go:fix
directives to facilitate migration.

Now let's remove the constructors before cutting the release.

Fixes #305
2025-09-10 11:21:46 -04:00
Rob Findley
5bbca40032 mcp: fix typo in protocolVersion20241105
This version was so last year.

Fixes #432
2025-09-09 11:21:18 -04:00
Clément RUFFINONI
bae3558d49
mcp: allow configurable terminate duration for CommandTransport (#363)
Make the process termination timeout configurable instead of using a
hardcoded 5-second delay. This allows applications to customize the
termination behavior based on their specific needs.

Fixes #322.
2025-09-08 13:28:32 -04:00
Rob Findley
4656930de8 mcp: avoid "null" when tools fail to return content
For some reason, this was a regression from v0.3.1, for some of our
examples. Fix it and address the existing TODO.
2025-09-05 16:57:02 -04:00
Robert Findley
40b6bd30f1
all: add examples of customizing tool schemas, and clarify documentation (#421)
Add example/server/toolschemas, which demonstrates various ways to
customize tool schemas, and illustrates the differences between
ToolHandler and ToolHandlerFor.

Fixes #385
Fixes #386
For #368
2025-09-05 14:11:11 -04:00
Rob Findley
5ccd97fafc mcp: allow requests before notifications/initialized
Upon a closer reading of the spec, it is OK for the server to response
to client requests before it has received notifications/initialized (as
long as it has received initialize). This effectively rolls back the fix
from #225.

Some hooks are left for enforcing strictness around server->client
requests prior to initialized. These will be revisited in subsequent
CLs.

For #395
2025-09-05 12:04:58 -04:00
Rob Findley
9d34aff732 mcp: avoid "null" in structuredContent
Avoid a typed nil in structuredContent (yet another typed nil), leading
to "null" on the wire.

Fixes #417
2025-09-04 19:48:51 -04:00
Rob Findley
ee5141621c mcp: be strict about returning the Mcp-Session-Id header
Rather than returning the Mcp-Session-Id header for all responses, only
return it from initialize, per the spec.

Fixes #412
2025-09-04 15:34:05 -04:00
Rob Findley
2c40bdc4d7 mcp: systematically improve streamable client errors
The streamable client connection can break for a variety of reasons,
asynchronously to the client's request. Decorate these failures with
additional context to clarify why they occurred.

Add a test for the failure message of #393.

Fixes #393
2025-09-04 14:09:37 -04:00
Rob Findley
a4313f92bf mcp: refactor the streamable client test to be more flexible
Use a fake streamable server to facilitate testing client behavior.
For this commit, just update the existing test (moved to a new file for
isolation). Subsequent CLs will add more tests.

Improve one client error message that occurred while debuging tests.

For #393
2025-09-04 14:09:37 -04:00
Jonathan Amsterdam
30261728f7
mcp: validate user-provided output schemas (#408)
toolForErr was ignoring the output schema if the output type was any.
That neglected the case where the user provided their own output schema.

Fixes #371.
2025-09-04 14:04:27 -04:00
Rob Findley
07b9cee8cc mcp: flush headers immediately for the hanging GET
Flush headers immediately for the persistent hanging GET of the
streamable transport; otherwise, clients may time out.

Fixes #410
2025-09-04 12:40:07 -04:00
Jonathan Amsterdam
203792dfdc
mcp: improve docs around tool handlers (#414) 2025-09-04 11:13:17 -04:00
ln-12
4257528dde
mcp: set default logging level to info (#411)
During initialization, the default logging level is set to info allowing
servers to emit logs immediately without waiting for a client to set a
log level.
The updated example shows how to send a notification in the context of
the request.

Fixes #387
2025-09-04 11:10:39 -04:00
Rob Findley
a1eb4849b7 mcp: correctly disallow GET requests on stateless servers
The condition sessionID == "" was not quite right for disallowing GET
requests. Since we decided to differentiate "stateless" vs "sessionless"
servers, we need to disallow GET requests for both.

For #393
2025-09-03 19:25:25 -04:00
Jonathan Amsterdam
8314ec099b
mcp: validate tool output (#352)
Validate the handler's returned output against the output schema of a tool.

Fixes #301.
2025-09-03 16:25:48 -04:00
Jonathan Amsterdam
d0c5943f25
add HTTP Request to TokenVerifier (#404)
The request might be needed to verify the token.

Fixes #403.
2025-09-03 16:23:42 -04:00