Commit graph

306 commits

Author SHA1 Message Date
Jonathan Amsterdam
032f03bbea
jsonschema: add ForType function (#242)
Add ForType, which is like For but takes a reflect.Type.

Fixes #233.
2025-08-05 16:32:06 -04:00
davidleitw
be0bd77f48
mcp: test nil params crash vulnerability
Test that explicit nulls don't cause panics.
2025-08-05 12:28:36 -04:00
Rob Findley
f4a9396942 mcp: fix a bug causing premature termination of streams
As reported in #227, there was a race where streams could be terminated
before the final reply was written. Fix this by checking the number of
outstanding requests in the same critical section that acquires outgoing
messages to write.

Fixes #227
2025-08-05 12:14:02 -04:00
Rob Findley
272a5ac76c .github: add race and 1.25 tests
This CL makes the following changes to our Github workflows, motivated
by #227:

- Add -race tests.
- Add go1.25rc2 to the build matrix.
- Check out code before setting up Go, so that the setup step can use
  dependency caching.
2025-08-05 12:14:02 -04:00
winterfx
31c18fba2d
docs: fix middleware example function signature (#236)
- Correct MethodHandler type parameter to use fully qualified
mcp.ServerSession
- Update parameter types to use proper mcp.Params and mcp.Result instead
of any
- Ensure example code matches the actual API
2025-08-05 12:05:51 -04:00
cryo
90a0b1fb23
mcp: fix comments (#241)
Update inaccurate doc comment for ClientSession.
2025-08-05 10:46:39 -04:00
Jim Clark
cb17593a5d
update Subscribe/Unsubscribe handlers in ServerOpts (#231)
## Background

Since the Server has a map of ServersSessions->subscriptions, and takes
care of routing ResourceUpdate messages to ServerSessions, I think the
decision was made to leave *ServerSession out of the
SubscribeHandler/UnsubscribeHandler signatures. However, resource
subscriptions are not specific to the a particular client and this
information is kept private in the Session today.

## Proposal

Align the Subscribe/Unsubscribe handlers with the other two handlers
that are client specific (ProgressNotificationHandler, and
RootListChangeHanlder), and include *ServerSession in the signature.

It comes in practice because because a gateway server that needs to
forward subscriptions still needs this information. The routing logic in
Server is convenient and means that most servers won't need to know
which ServerSessions are subscribed. But I think there are still use
cases where the user will need to know.
2025-08-04 14:46:49 -04:00
Jonathan Amsterdam
1e40ede369
jsonschema: options for inference (#185)
- Add ForOptions to hold options for schema inference.

- Replace ForLax with ForOptions.IgnoreBadTypes.

- Add an option to provide schemas for arbitrary types.

- Provide a default mapping from types to schemas that includes stdlib
types with MarshalJSON methods.

- Add Schema.CloneSchemas. This is needed to make copies of the schemas
in the above map: a schema cannot appear twice in a parent schema,
because schema addresses matter when resolving internal references.
2025-08-04 10:50:57 -04:00
Sam Thanawalla
faaf77dc05
mcp/server: advertise completions only if installed (#213)
Previously, completions would always be advertised as a capability even
if the CompletionHandler was not installed by the server. This CL fixes
that.
2025-08-04 10:12:01 -04:00
cryo
0cde9623d3
mcp: fix comments (#221)
### change:
Fix typos in some comments.
2025-08-01 16:20:55 -04:00
Robert Findley
b34ba2149d
mcp: handle bad or missing params (#210)
Audit all cases where params must not be null, and enforce this using
methodInfo via a new methodFlags bitfield that parameterizes method
requirements. Write extensive conformance tests catching all the
(server-side) crashes that were possible.

We should go further and validate schema against the spec, but that is
more indirect, more complicated, and potentially slower. For now we
adopt this more explicit approach.

Still TODO in a subsequent CL: verify the client side of this with
client conformance tests.

Additionally, improve some error messages that were redundant or leaked
internal implementation details.

Fixes #195
2025-07-31 10:57:59 -04:00
Sam Thanawalla
a9a503f74a
mcp/streamable: add persistent SSE GET listener (#206)
This CL adds the optional persistent SSE GET listener as specified in
section 2.2.
https://modelcontextprotocol.io/specification/2025-06-18/basic/transports#listening-for-messages-from-the-server

This enables server initiated SSE streams.

For #10
2025-07-30 17:00:50 -04:00
Robert Findley
56734edf46
mcp: reject notifications with unexpected ID field (#204)
Use the new 'isRequest' field on methodInfo to also reject notifications
with an unexpected ID field.

For #196
2025-07-30 15:19:51 -04:00
Robert Findley
9f48a04971
mcp: remove unused functions (cleanup) (#207)
Remove a few functions that were flagged by gopls as unused. We can
always add them back if necessary.
2025-07-30 14:59:47 -04:00
Francis Zhou
bacca7a1d2
Implement json.Unmarshaler to CreateMessageResult (#191)
When calling `ServerSession.CreateMessage`, will get an json unmarshal
error because `CreateMessageResult.Content` is an interface, and it
doesn't implement `json.Unmarshaller`.

Implement the `json.Unmarshaller` to `CreateMessageResult`, let it able
to unmarshal the client response without error
2025-07-30 13:25:06 -04:00
CSK
982a0bceca
feat(devcontainer): Adds Dev Container configuration (#154)
This fixes #151, which makes it easier for users to easily use GitHub
Codespaces, providing them with a VS Code like environment with their
preferred settings/themes and the required dependencies, all within the
browser.
2025-07-30 12:54:41 -04:00
Jonathan Amsterdam
0fa4a6c65b
CONTRIBUTING.md: timeout policy (#187)
Explain that we'll close issues or PRs after a period of inactivity.

This will reduce clutter on the issue and PR pages.
2025-07-30 12:53:56 -04:00
Robert Findley
64493be392
mcp: reject requests with bad IDs (#202)
Add an isRequest field to methodInfo, used it to reject non-notification
requests that lack a valid ID. Furthermore, lift this validation to the
transport layer for HTTP server transports, so that we can preemptively
reject bad HTTP requests.

Fixes #194
Fixes #197
2025-07-30 12:26:40 -04:00
Jonathan Amsterdam
3ac4ca9c5b
mcp: streamable.go: clarifications (#189)
Clarify or remove TODOs.

Make some code clearer.
2025-07-29 10:49:16 -04:00
Jonathan Amsterdam
64b5b9125e
mcp: support application/json in streamable client (#181)
The client handles POST responses with content type application/json.

For: #10
Fixes #129
2025-07-29 10:28:49 -04:00
Sam Thanawalla
2b6f7b5185
mcp/server: add initial capability server option (#182)
This CL adds an option for servers to advertise support for features
that have not been added yet. This includes Prompts, Tools, and
Resources as these could be dynamically added after initialization.

Fixes: #135.
2025-07-29 05:20:08 -04:00
Jonathan Amsterdam
a5aa370ea0
mcp: various cleanups to streamable code (#184)
- move done field outside of mutex hat
- refactor handler into a function that returns `error` to avoid repeated `http.Error` calls
- use an atomic for the signal channel to simplify locking
2025-07-28 13:58:37 -04:00
Koichi Shiraishi
619bc41581
internal/jsonrpc2,mcp: fix typos (#183)
- add links to doc strings
- use correct symbol and package names
2025-07-28 12:18:03 -04:00
Jonathan Amsterdam
261a63ccc0
mcp: don't panic on bad JSON RPC input (#177)
Log internal errors from the jsonprc2 package, instead of panicking.

Fixes #175.
2025-07-28 10:29:59 -04:00
Jonathan Amsterdam
6e5ba9585f
mcp: remove create-repo.sh (#178)
It was a one-off script whose purpose has been served.
2025-07-28 10:28:01 -04:00
Jonathan Amsterdam
86f71fcb70
mcp: add stream type (#171)
Consolidate several maps into a single struct.

Simplifies the code, for the most part.
2025-07-28 10:27:02 -04:00
cryo
e8c6e03841
fix comment (#180)
update comment for func `clientMethod`
2025-07-28 07:51:49 -04:00
Adam Koszek
1ea7df3267
mcp/examples: Make example more usable. (#160)
I'm making this example more useful for debugging:
- `-port` is supported
- `-host` is supported
- error handling is there.
2025-07-27 08:23:52 -04:00
Jonathan Amsterdam
c879ac3c61
mcp: gracefully handle a nil server in handlers (#164)
If the getServer function passed to NewSSEHandler or
NewStreamableHTTPHandler returns nil, serve a 400 instead of panicking.

Fixes #161.
2025-07-24 10:35:14 -04:00
Jonathan Amsterdam
8dd9a819bb
mcp: panic if AddTool method is given a nil InputSchema (#157)
The spec requires an input schema for every tool.
2025-07-23 16:02:00 -04:00
Sam Thanawalla
94b88a26e0
mcp/server: fix nil PingParams in server.Ping (#163)
Use OrZero in case PingParams is nil to avoid typed nil.

Fixes: #162
2025-07-23 15:40:57 -04:00
Sam Thanawalla
bcbb31f072
mcp: add resource subscriptions (#138)
This CL adds the ability for clients to subscribe and receive updates
for resources as described in
https://modelcontextprotocol.io/specification/2025-06-18/server/resources#subscriptions

Fixes: #23
2025-07-23 15:23:50 -04:00
Jonathan Amsterdam
ea6162ca1c
mcp: incorporate EventStore into StreamableServerTransport (#156)
Use the EventStore interface to implement resumption on the server side.
2025-07-23 13:42:50 -04:00
ln-12
4529904c7a
Fix minor issues in design.md (#158)
design.md: minor fixes
2025-07-23 07:17:57 -04:00
Sam Thanawalla
a911cd0ffd
mcp/streamable: add resumability for the Streamable transport (#133)
This CL implements a retry mechanism to resume SSE streams to recover
from network failures.

For #10
2025-07-22 15:58:29 -04:00
Jonathan Amsterdam
1465442b25
mcp: add EventStore (#152)
Introduce an EventStore interface to store events for resumable streams.

Provide an in-memory implementation.

Still to do: connect to streaming transports.

For #10
2025-07-22 10:21:05 -04:00
Jonathan Amsterdam
9ddad03891
mcp: dedup wire type for image and audio (#149)
Factor out the wire type into a top-level struct.
2025-07-21 11:40:32 -04:00
Jonathan Amsterdam
c6fabbb3d3
jsonschema: inference ignores invalid types (#147)
Add ForLax[T], which ignores invalid types in schema inference instead
of returning an error.

This allows additional customization of a schema after inference does
what it can.

For example, an interface type where all the possible implementations
are known can be described with "oneof".

For #136.
2025-07-21 11:18:04 -04:00
Jonathan Amsterdam
a519c182e8
mcp: export Event (#145)
Export the Event type in preparation for providing user-definable
storage for resumable streams.

Also, move event code to a separate file.

This PR is just renaming and code motion.
2025-07-21 10:54:59 -04:00
Jonathan Amsterdam
3bbe74f8a5
jsonschema: make schema state independent of resolve arguments (#85)
Move schema state that depends on resolution into the Resolved struct.
The remaining unexported schema fields are dependent only on the schema
itself and its sub-schemas.

Now a schema can be resolved multiple times, as itself or as part of
other schemas.

Fixes #84.
2025-07-21 10:41:00 -04:00
Martin Emde
dced3e4503
mcp: don't omit required fields in ImageContent and AudioContent (#95)
Update `ImageContent` and `AudioContent` to use an inline wire format
to ensure that required fields are not omitted during JSON marshaling,
matching TypeScript schema requirements. This follows the TextContent
approach.

- `ImageContent` requires `data` and `mimeType` fields.
- `AudioContent` requires `data` and `mimeType` fields.
2025-07-19 17:08:56 -04:00
CSK
d437c81cbe
docs: Enhance README formatting and fix relative links (#146)
This PR improves the formatting and internal link structure of both the
main `README.md` and the source file at `internal/readme/README.src.md`.
Changes include:

### 🛠 Changes Made

* Replaced plain-text warning with a GitHub-style admonition block (`>
[!WARNING]`) for improved visibility and consistency with other docs.

Example:

> [!WARNING]
> The SDK should be considered unreleased, and is currently unstable
> and subject to breaking changes. Please test it out and file bug
reports or API
> proposals, but don't use it in real projects. See the issue tracker
for known
> issues and missing features. We aim to release a stable version of the
SDK in
> August, 2025.

* Updated markdown links to use cleaner relative paths:
* Changed `CONTRIBUTING.md` reference to
`[CONTRIBUTING.md](/CONTRIBUTING.md)`
  * Updated `examples/` reference to `[examples/](/examples/)`
* Ensured that all updates were mirrored in both the generated and
source README files.

### 📌 Why It Matters

These changes improve the readability and usability of the
documentation, especially when viewed on GitHub. The updated formatting
helps call attention to important warnings, and fixing relative paths
ensures that links work reliably across platforms and contexts.
2025-07-18 08:43:47 -04:00
Robert Jackson
dd49b7039b
mcp: Add example of using Middleware for logging purposes (#58)
I needed to implement some logging for our experimental server that we
are building out using the SDK, and figured I could propose an example
that might make it easier for folks in the future.

For #33.
2025-07-17 10:44:15 -04:00
Jonathan Amsterdam
de4b788e59
mcp: fix conformance tests (#132)
- Use new AddXXX API.

- Update goldens to reflect new capability logic.

Fixes #131.
2025-07-14 12:47:58 -04:00
Qihang Hu
bf7e518215
docs: fix the service example in the readme and examples/hello (#127)
Fix an example using mcp tag instead of jsonschema.
2025-07-14 11:39:30 -04:00
Robert Findley
a26e1dc253
mcp: fail when streamable http client encounters unsupported content (#130)
The streamable HTTP client is not fully implemented, but did not fail
with meaningful error messages when encountering unsupported content
types from other servers. This may be straightforward to fix, but for
now at least provide a meaningful error message.

For modelcontextprotocol/go-sdk#129.
2025-07-14 10:02:33 -04:00
Ryuji Iwata
b780f1b3e1
Fix jsonrpc path and url (#128)
jsonrpc's github path notation and pkg.go.dev link url are broken.
2025-07-14 08:21:30 -04:00
Naoki Sega
d570022d03
mcp: add sequential thinking server example (#51)
Add example of implementing mcp on [sequentialthinking
server](https://github.com/modelcontextprotocol/servers/tree/main/src/sequentialthinking),
from [Example Servers](https://modelcontextprotocol.io/examples).

The PR includes dynamic and reflective problem-solving through
structured thinking processes.
The server provides tools for:
- starting thinking sessions
- adding sequential thoughts with step tracking
- revising previous thoughts
- creating alternative reasoning branches
-  reviewing complete processes.

Also, features include thread-safe session management, adaptive planning
that adjusts step counts dynamically.
2025-07-14 08:12:38 -04:00
Vid Drobnič
fe208c144f
jsonrpc: expose encoding and decoding functions (#114)
Expose `jsonrpc2.MakeID`, `jsonrpc2.EncodeMessage` and
`jsonrpc2.DecodeMessage` functions to allow implementing custom
`mcp.Transport`. 

Add an example that demonstrates a custom transport
implementation.

Fixes #110.
2025-07-14 07:01:40 -04:00
Michael Pratt
aa9d4b28be
jsonschema: add missing colon to link block (#126)
Without this, not only is the one link not valid, the entire section is
considered invalid and none of the link references are used.
2025-07-11 17:34:18 -04:00