Commit graph ruby-sdk/lib/mcp
Author SHA1 Message Date
Koichi ITO
b626425521 Remove useless magic comment
This PR removes useless magic comment.
2025-09-15 12:33:09 +09:00
Ates Goral
deebefd26f Release v0.3.0
🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-14 01:09:18 -04:00
Ateş Göral
cd4681351d
Merge branch 'main' into main 2025-09-13 17:11:23 -04:00
Ateş Göral
5c5f420f42
Merge pull request #122 from koic/add_validation_for_tool_annotation
Validate tool annotations value
2025-09-13 14:22:42 -04:00
Valentino Stoll
aad8e94530
Add Tool output_schema support with comprehensive validation
This commit implements optional JSON Schema validation for tool outputs according to the MCP specification:

- Add MCP::Tool::OutputSchema class mirroring InputSchema functionality
- Support output_schema in class definitions, Tool.define, and OutputSchema objects
- Include validate_result method for runtime validation
- Update MCP::Client::Tool to track output schemas
- Add comprehensive test coverage with 249 passing tests
- Update README with dedicated Tool Output Schemas section
- Follow MCP spec requirements for server/client validation
2025-09-11 11:12:27 -04:00
Koichi ITO
08048283e4 Validate tool annotations value
Tool annotations were introduced in the "2025-03-26" specification:

> Added comprehensive tool annotations for better describing tool behavior,
> like whether it is read-only or destructive (PR modelcontextprotocol/modelcontextprotocol#185)

https://modelcontextprotocol.io/specification/2025-03-26/changelog#major-changes

This means they are not supported in the "2024-11-05" specification,
so validation errors will be raised for `protocol_version: "2024-11-05"`.
2025-09-11 10:48:56 +09:00
Koichi ITO
dd5b318b11
Merge pull request #126 from koic/fix_errors_when_title_is_not_specified
Fix errors when `title` is not specified
2025-09-11 10:30:15 +09:00
Koichi ITO
97640ed626
Merge pull request #28 from jcat4/add-basic-http-client-support
Add basic http client support
2025-09-09 18:51:07 +09:00
Joey Cardosi
8b764428b1
Add basic HTTP client support with pluggable transports 2025-09-08 15:52:37 -05:00
Koichi ITO
b9f1c58899 Combine redundant any? calls
Calling `parameters.any?` twice is redundant.
2025-09-07 10:04:18 +09:00
Koichi ITO
47d29e8014 Fix errors when title is not specified
Since the optional `title` becomes `title: null` when not provided, the following error occurs.
This occurs with Claude Code 1.0.100.

```console
[DEBUG] MCP server "example": Connection failed after 1591ms: [
  {
    "code": "invalid_type",
    "expected": "string",
    "received": "null",
    "path": [
      "serverInfo",
      "title"
    ],
    "message": "Expected string, received null"
  }
]
```

By adding `compact`, the `title` key will be removed if the optional `title` keyword argument is not specified.
2025-09-03 12:31:33 +09:00
Koichi ITO
b982074daa
Merge pull request #119 from koic/add_title_to_server_info
Add `title` to `serverInfo`
2025-08-30 02:01:53 +09:00
Koichi ITO
ef56280518 Add title to serverInfo
Follow-up to #109.

`title` has also been added to `serverInfo` and `clientInfo` by
https://github.com/modelcontextprotocol/modelcontextprotocol/pull/663.

Since `clientInfo` is not treated as product code, support is implemented only in `serverInfo`.

See also https://modelcontextprotocol.io/specification/2025-06-18/schema#implementation.
2025-08-29 14:36:39 +09:00
Koichi ITO
cae30547d2 Set default values for ToolAnnotations hints
According to the specification, `destructive_hint`, `idempotent_hint`, `open_world_hint`,
and `read_only_hint` of `ToolAnnotations` are optional with default values:
https://modelcontextprotocol.io/specification/2025-06-18/schema#toolannotations

This PR sets their default values in accordance with the specification.

Additionally, for readability, the hints have been reordered to follow the specification.
2025-08-28 01:13:57 +09:00
Jens Kraemer
be5e7db2a6 implements the notifications/initialized method
- According to
  https://modelcontextprotocol.io/specification/2025-06-18/basic/lifecycle,
  "After successful initialization, the client MUST send an initialized
  notification to indicate it is ready to begin normal operations".
2025-08-25 13:44:21 +08:00
Topher Bullock
62360338f4
Merge pull request #109 from jdnvn/main
Add title to tools, prompts and resources
2025-08-20 16:19:38 -04:00
Koichi ITO
002df2c774
Merge pull request #107 from pusewicz/response-keyword-boolean
Response error keyword argument
2025-08-20 18:26:17 +09:00
Piotr Usewicz
6689db2ca0
Nicer error API
Both the combination of positional argument and naming does not feel
like Ruby.

This change makes `is_error` argument a keyword argument called `error`.

Thanks to this, the usage is more self-explanatory:

```ruby
response = Response.new([], error: true)
```
2025-08-18 16:50:20 +02:00
jdnvn
8b4085468e add titles to tools, prompts and resources 2025-08-17 22:58:15 -04:00
Koichi ITO
d1c6a46e1a
Merge pull request #105 from siddhantbajaj/handle-notification-initialized-method
Return HTTP status code 202 for notification initialized method
2025-08-13 00:38:36 +09:00
Siddhant Bajaj
4a65f62158 Return HTTP status code 202 for notification initialized method 2025-08-11 10:40:17 -04:00
Koichi ITO
930173a04e
Merge pull request #87 from koic/add_instructions_support_to_mcp_server
Add `instructions` support to `MCP::Server`
2025-08-09 03:22:08 +09:00
Koichi ITO
f6f064ab71 Add instructions support to MCP::Server
The response of the `init` method can include `instructions`.
https://modelcontextprotocol.io/specification/2025-06-18/basic/lifecycle

`instructions` appear to be supported starting from the 2025-03-26 specification.

- https://modelcontextprotocol.io/specification/2025-03-26/basic/lifecycle (supports `instructions`)
- https://modelcontextprotocol.io/specification/2024-11-05/basic/lifecycle (does not support `instructions`)

If `instructions` is used with an unsupported protocol version, an `ArgumentError` will be raised.

As additional context, the TypeScript SDK also treats `instructions` as optional.
https://github.com/modelcontextprotocol/typescript-sdk/blob/1.16.0/src/types.ts#L356-L361
2025-08-07 12:17:57 +09:00
Piotr Usewicz
2508defc79
inputSchema interface requires type key
When defining a tool without any arguments taken, it should be allowed
to not have to define the `input_schema` at all, and the default value
for such a scenario should be `{ type: "object" }` as per the schema.

16d91abb68/schema/2025-06-18/schema.ts (L896-L900)
2025-07-31 08:36:18 +02:00
dak2
e7fafddf94 Fix JSON-RPC notification format in Streamable HTTP transport
Update send_notification method to properly construct JSON-RPC notification objects with jsonrpc version and method fields.
This ensures notifications conform to the JSON-RPC 2.0 specification when sent over HTTP transport.

https://www.jsonrpc.org/specification#notification

Another goal is to standardise the `send_notification` interface.
Before the revision, it was sufficient to pass the method and parameters in the case of stdio.
However, in the case of Streamable, a hash based on the JSON-RPC specification must be passed.
2025-07-26 17:58:31 +09:00
calin
47215c98ce Account for missing arguments 2025-07-23 15:17:23 +03:00
Sheldon Nunes
74737223a0 Fix tools with no args 2025-07-23 13:42:19 +12:00
Koichi ITO
2b36bf6b2d Set default protocol version to the latest specification
Currently, MCP has three versions: 2024-11-05, 2025-03-26, and 2025-06-18.

- https://modelcontextprotocol.io/specification/2024-11-05/basic/transports (stdio and HTTP with SSE)
- https://modelcontextprotocol.io/specification/2025-03-26/basic/transports (stdio and Streamable HTTP)
- https://modelcontextprotocol.io/specification/2025-06-18/basic/transports (stdio and Streamable HTTP)

The newer specification supports stdio and Streamable HTTP as transport mechanisms.
Only the oldest version, 2024-11-05, supports stdin and HTTP with SSE,
but that version is now considered legacy.

Given the current behavior of the Ruby SDK, it likely makes more sense to set
the default protocol version to the latest one, 2025-06-18, rather than the oldest, 2024-11-05.

Ideally, the implementation should adjust its behavior based on the version,
but this PR is limited to updating the default protocol version only.
2025-07-20 16:36:18 +09:00
Koichi ITO
f13522d416 Validate protocol_version value
## Motivation and Context

Currently, MCP supports the following three protocol versions:
"2025-06-18", "2025-03-26", and "2024-11-05".
https://modelcontextprotocol.io/introduction

As shown in the changes to `test/mcp/configuration_test.rb`,
the `protocol_version` value is not currently being validated,
which allows unexpected `protocol_version` values to be accepted.

This PR ensures that only supported protocol versions are accepted.

## How Has This Been Tested?

Existing tests have been updated and new tests have been added.

## Breaking Changes

None. If it becomes an issue, it means a value that was not originally intended has been used.

## Additional context

While one possible approach might be to fall back to a default protocol version
when an invalid `protocol_version` value is provided,
this PR opts to reject invalid `protocol_version` values explicitly.
The reasoning is that encouraging users to provide intentional,
valid input is preferable to silently falling back to a default,
which could lead to unexpected behavior.
2025-07-16 12:38:49 +09:00
Ates Goral
d234d011e6 Bump version 2025-07-15 15:40:15 -04:00
Ates Goral
1ed313951a Add define_custom_method to define custom methods 2025-07-14 20:12:29 -04:00
Koichi ITO
13bee9f60e
Merge pull request #63 from kfischer-okarin/check-notification-capabilities
Check all methods & capabilities
2025-07-13 11:34:42 +09:00
Koichi ITO
1a89cc5537
Merge pull request #61 from kfischer-okarin/simplify-and-add-missing-capabilities
Remove automatic capability determination and add missing capabilities
2025-07-12 19:01:35 +09:00
Jens Kraemer
271ccb43d0 fixes input schema validation for schemas without required fields 2025-07-11 11:27:59 +02:00
Juliano Julio Costa
5d0c5b7f0e Merge remote-tracking branch 'upstream/main' into make_server_context_optional_and_sorbet_friendly 2025-06-19 11:08:00 -04:00
Kevin Fischer
d7c866efcf
Simplify capability handling again 2025-06-18 13:23:25 +09:00
Topher Bullock
8dda58e77c
Merge pull request #43 from tylerrowsell/tool-arg-validation
Validate ToolCall Args against Schema
2025-06-17 17:39:13 -04:00
Sam Bostock
ffa06b103d
Merge pull request #64 from sambostock/duplicate-method-instrumentation
Remove duplicate `method` instrumentation
2025-06-17 11:58:36 -04:00
Koichi ITO
0027ce7126
Merge pull request #51 from koic/add_migration_path_for_server_transports_std_transport
Add a migration path for `Server::Transports::StdioTransport`
2025-06-18 00:25:00 +09:00
Sam Bostock
ce1c8719cf
Remove duplicate method instrumentation
`handle_request` already adds the `method` to the instrumentation data,
so there is no need to repeat this. Moreover, consumers can override
these anyway.
2025-06-17 02:07:25 -04:00
Kevin Fischer
ff92a3d016
Add missing checks 2025-06-15 17:35:18 +09:00
Kevin Fischer
280e835184
Refactor capability check 2025-06-15 17:35:18 +09:00
Kevin Fischer
9a00406fae
Rewrite extend self to class << self 2025-06-15 17:35:18 +09:00
Kevin Fischer
127f12f740
Add missing method constants 2025-06-15 17:35:18 +09:00
dak2
dc38c9f84a Rename to MCP when handling exceptions in Streamable HTTP transport
The reason for the change is that a NameError occurs when a POST request is made while the stream is closed because ModelContextProtocol is undefined.

I have also added test cases to check the behaviour when IOError and Errno::EPIPE occurs.
2025-06-15 15:21:22 +09:00
Riley Shott
4d79263a87 Addressing feedback 2025-06-11 13:44:54 -07:00
Juliano Julio Costa
c10c0ddf9d Refactor MCP tool and server to handle optional server_context parameter
- Updated `call` method in `MCP::Tool` and `MCP::Prompt` to accept `server_context` as an optional parameter.
- Refactored `MCP::Server` to streamline argument handling for tool and prompt calls, introducing helper methods `call_tool_with_args` and `call_prompt_template_with_args`.
- Adjusted tests to reflect changes in method signatures and ensure compatibility with optional `server_context` parameter.
2025-06-10 15:40:28 -04:00
Koichi ITO
16ab4bac81 Add a migration path for Server::Transports::StdioTransport
Follow-up to #27.

This PR adds a migration path for `Server::Transports::StdioTransport`. e.g.,

```console
$ cat /tmp/example.rb
require "mcp/transports/stdio"

MCP::Transports::StdioTransport
```

This change displays the following warning instead of raising an error for such cases, allowing execution to proceed:

```console
$ bundle exec ruby /tmp/example.rb
/tmp/example.rb:1: warning: Use `require "mcp/server/transports/stdio_transport"` instead of `require "mcp/transports/stdio"`.
Also use `MCP::Server::Transports::StdioTransport` instead of `MCP::Transports::StdioTransport`.
This API is deprecated and will be removed in a future release.
```

Currently (v0.1.0), it results in an error without any warning.
This MCP gem is not yet close to version 1.0, but introducing breaking changes to a published interface is undesirable.

This compatibility code should be removed at some point after a few releases.

Closes #50.
2025-06-10 16:27:22 +09:00
Riley Shott
91af9ad19a Gracefully handle POST requests with nil bodies 2025-06-09 14:14:41 -07:00
Riley Shott
55b03fc7aa Add notification support
Co-authored-by: Kevin Fischer <kfischer_okarin@yahoo.co.jp>
2025-06-09 14:12:13 -07:00