Commit graph

431 commits

Author SHA1 Message Date
Rob Findley
c2c810bf40 mcp: update jsonschema-go to v0.2.3, prepare README v0.5.0 2025-09-12 12:00:54 -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
Rob Findley
eddef064df all: add a test that checks for missing or mismatching file copyright
In #294, we accidentally checked files with the default Go project
copyright header. Avoid this in the future with a copyright test.
2025-09-11 13:53:22 -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
Jonathan Amsterdam
a082e0764d
go.mod: update to jsonschema v0.2.1 2025-09-09 10:34:38 -04:00
Robert Findley
4f197bc3b4
internal/oauthex: fix license headers (#430)
The files in the oauthex package used the default Go license header,
rather than the MCP SDK license header (an accident of tooling). We
should fix this while jba@google.com is the only author of this code.

A subsequent PR will add a lint check to prevent this in the future.
2025-09-08 13:36:50 -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
Flc゛
e06cc69dc4
fix(docs): correct number in README (#427) 2025-09-08 02:11:31 -04:00
Rob Findley
4656930de8 mcp: avoid "null" when tools fail to return content v0.4.0
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
46e767e646
Update README for v0.4.0 (#422)
As we prepare to release v0.4.0, update the README to reference the new
version.
2025-09-05 16:30:59 -04:00
Jonathan Amsterdam
2a00640e1e
mcp/internal/oauthex: auth server metadata (#294)
Implement the Authorization Server Metadata spec.
2025-09-05 14:17:21 -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
Kason Braley
b774809086
examples: Update and fix outdated (#420)
Update examples/server/rate-limiting to use the same replace directive
strategy as the auth-middleware example, and fix the breakages.

Also remove the unneeded type parameters in examples/client/middleware.
2025-09-05 07:58:26 -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
92e19c1be8
examples/server/auth-middleware: remove custom middleware (#405)
The additional middleware added the TokenInfo the context.
RequireBearerToken already does that.
2025-09-03 17:27:38 -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
Jonathan Amsterdam
29c1650e2a
internal/oauthex: OAuth extensions (#125)
Add a package for the extensions to OAuth 2.0 required by MCP.

This first PR adds Protected Resource Metadata.
2025-09-03 14:20:53 -04:00
Shusaku Yasoda
1250a31b51
auth: add OAuth error handling in TokenVerifier and tests (#399)
Add ErrOAuth error type and handling to match TypeScript SDK behavior.
OAuth protocol errors now return HTTP 400 instead of 500, providing
better error classification for authentication issues.

Changes

- Add ErrOAuth variable for OAuth-specific protocol errors
- Update verify function to return 400 for OAuth errors
- Add test case for OAuth error handling


Fixes compatibility with TypeScript SDK error handling patterns.
2025-09-03 13:05:07 -04:00
ccpro
1c20560beb
mcp: export jsonResponse field
Exporting StreamableHTTPOptions.JSONResponse is helpful for some users behind proxies that don't work well with text/event-stream.

For #211

Co-authored-by: ccpro10 <co1797249167@gmail.com>
2025-09-03 11:35:19 -04:00
Robert Findley
c76d991f05
examples/server/distributed: address comment in #380
I failed to push my branch before merging #380. Address one minor
comment from that PR.
2025-09-03 11:34:39 -04:00
Jonathan Amsterdam
0bb1a42b53
examples/server/middleware: log tool result (#400)
Log the result of a tool call in the middleware.

This example now fully demonstrates that receiving middleware can
effectively wrap a ToolHandler. That means that one reason for ToolFor
is moot: you don't need to get your hands on the returned ToolHandler in
order to wrap it.
2025-09-03 11:34:02 -04:00
Jonathan Amsterdam
063fb12149
mcp: unexport ToolFor (#402)
The rationale is provided on the issue.

Also, document the low-level nature of Server.AddTool. (drive-by change)

Fixes #401.
2025-09-03 11:33:40 -04:00
Jonathan Amsterdam
7769b2a959
mcp: set content to marshaled output (#398)
The ToolHandler constructed by ToolFor sets the result's Content to the
marshaled output, following the spec's suggestion.

See
https://modelcontextprotocol.io/specification/2025-06-18/server/tools#structured-content.

Also, set StructuredContent to the marshaled RawMessage to avoid a
double marshal.

Fixes #391.
2025-09-03 10:37:27 -04:00
Robert Findley
07b65d76a7
examples/server/distributed: a stateless distributed server
Add an example of a server that proxies HTTP requests to multiple
stateless streamable MCP backends.

For #148
2025-09-02 14:32:12 -04:00
Brad Hoekstra
24fc13eb88
cleanup: remove reference to deleted jsonschema package
Signed-off-by: Brad Hoekstra <bhoekstra@google.com>
2025-09-02 14:19:53 -04:00
Shusaku Yasoda
a3935c6be2
examples/server: add example MCP server with JWT and API key (#339)
This commit introduces a new example demonstrating the integration of
authentication middleware with an MCP server. The server supports both
JWT token and API key authentication, along with scope-based access
control for various MCP tools. Key features include token generation
endpoints, in-memory API key storage, and a health check endpoint.

New files added:
- `main.go`: Implements the MCP server and authentication logic.
- `go.mod` and `go.sum`: Manage dependencies for the project.
- `README.md`: Provides setup instructions, available endpoints, and
example usage.

This example serves as a reference for implementing secure access to MCP
tools.

Fixes #330
2025-09-02 11:49:46 -04:00
Jonathan Amsterdam
be0a00cd0a
examples/http: update to v0.3.0
Fix build breakage.

Fixes #383.
2025-09-01 09:24:30 -04:00
Adam Koszek
a617dcea31
mcp/examples: HTTP server example with a simple client built-in (#168)
I'm adding a practical example of an MCP with HTTP streaming: both
client and server. Those are useful for testing real-world applications.
2025-08-30 14:30:48 -04:00
Jonathan Amsterdam
7bfde44981
mcp: statically type the server-side tool params (#378)
Introduce CallToolParamsRaw on the server side, so that tool handlers
see in the type system that a tool's arguments are a json.RawMessage.

Fixes #377.
2025-08-30 08:16:06 -04:00
Robert Findley
8f11a86898
examples/client: add a loadtest command v0.3.1
Add a loadtest client example, to help confirm performance of our
streamable transport implementation.

For #190
2025-08-29 15:28:05 -04:00
Sam Thanawalla
ddaf35ed77
mcp/streamable: use event store to fix unbounded memory issues (#335)
This CL utilizes the event store to write outgoing messages and removes
the unbounded outgoing data structure.

It also adds a new interface [EventStore.Open]

For #190
2025-08-29 12:54:51 -04:00
Huabing (Robin) Zhao
f6118aaace
mcp: fix the type of the Complete handler
The Complete handler was returning an abstract Result type, rather than concrete CompleteResult type.

Fixes #375
2025-08-29 10:06:23 -04:00
Takeshi Yoneda
a76bae3a11 address review comments
Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
2025-08-27 16:03:32 -04:00