## Summary Unlike applications, libraries provide value by remaining usable for a broad range of users, even when that means supporting older Ruby versions beyond Ruby's own EOL timeline. I maintain RuboCop, a Ruby linter. Because linters need to work across the ecosystem, they often need to keep compatibility with older Ruby versions to some extent. The trade-off is that the MCP Ruby SDK must remain compatible with Ruby 2.7. However, given the ecosystem impact, supporting Ruby 2.7 is still worthwhile. For example, bundled gems such as `csv` (Ruby >= 2.5), `bigdecimal` (Ruby >= 2.5), `json` (Ruby >= 2.7), and `language_server-protocol` (Ruby >= 2.5) tend to keep compatibility with older Ruby versions. Unlike dropping support, which imposes restrictions on users, this change broadens compatibility and does not introduce breaking changes. ## Development Note `rubocop-shopify` (2.18) supports Ruby 3.1+, and this PR does not change that. Since RuboCop can configure the runtime Ruby version and the target Ruby version for analysis independently, CI continues to run RuboCop on Ruby 4.0, while analyzing code with `TargetRubyVersion: 2.7`. When setting `TargetRubyVersion: 2.7`, several RuboCop offenses were resolved. Additionally, Sorbet-related libraries support Ruby 3.0+. Since these are not dependencies of the MCP Ruby SDK itself, they are versioned and tested in the development Gemfile.
4 KiB
4 KiB
AGENTS.md
Project overview
This is the official Ruby SDK for the Model Context Protocol (MCP), implementing both server and client functionality for JSON-RPC 2.0 based communication between LLM applications and context providers.
Dev environment setup
- Ruby 3.2.0+ required to run the full test suite, including all Sorbet-related features
- Run
bundle installto install dependencies - Dependencies:
json-schema>= 4.1 - Schema validation
Build and test commands
bundle install- Install dependenciesrake test- Run all testsrake rubocop- Run linterrake- Run tests and linting (default task)ruby -I lib -I test test/path/to/specific_test.rb- Run single test filegem build mcp.gemspec- Build the gem
Testing instructions
- Test files are in
test/directory with_test.rbsuffix - Run full test suite with
rake test - Run individual tests with
ruby -I lib -I test test/path/to/file_test.rb - Tests should pass before submitting PRs
Code style guidelines
- Follow RuboCop rules (run
rake rubocop) - Use frozen string literals
- Follow Ruby community conventions
- Keep dependencies minimal
Commit message conventions
- Use conventional commit format when possible
- Include clear, descriptive commit messages
- Releases are triggered by updating version in
lib/mcp/version.rband merging to main
Release process
- Follow Keep a Changelog format in CHANGELOG.md
- Update CHANGELOG.md before cutting releases
- Use git history and PR merge commits to construct changelog entries
- Format entries as: "Terse description of the change (#nnn)"
- Keep entries in flat list format (no nesting)
- Git tags mark commits that cut new releases
- Exclude maintenance PRs that don't concern end users
- Check upstream remote for PRs if available
Architecture overview
Core Components
MCP::Server (lib/mcp/server.rb):
- Main server class handling JSON-RPC requests
- Implements MCP protocol methods: initialize, ping, tools/list, tools/call, prompts/list, prompts/get, resources/list, resources/read
- Supports custom method registration via
define_custom_method - Handles instrumentation, exception reporting, and notifications
- Uses JsonRpcHandler for request processing
MCP::Client (lib/mcp/client.rb):
- Client interface for communicating with MCP servers
- Transport-agnostic design with pluggable transport layers
- Supports tool listing and invocation
Transport Layer:
MCP::Server::Transports::StdioTransport- Command-line stdio transportMCP::Server::Transports::StreamableHttpTransport- HTTP with streaming supportMCP::Client::HTTP- HTTP client transport (requires faraday gem)
Protocol Components:
MCP::Tool- Tool definition with input/output schemas and annotationsMCP::Prompt- Prompt templates with argument validationMCP::Resource- Resource registration and retrievalMCP::Configuration- Global configuration with exception reporting and instrumentation
Key Patterns
Three Ways to Define Components:
- Class inheritance (e.g.,
class MyTool < MCP::Tool) - Define methods (e.g.,
MCP::Tool.define(name: "my_tool") { ... }) - Server registration (e.g.,
server.define_tool(name: "my_tool") { ... })
Schema Validation:
- Tools support input_schema and output_schema for JSON Schema validation
- Protocol version 2025-03-26+ supports tool annotations (destructive_hint, idempotent_hint, etc.)
- Validation is configurable via
configuration.validate_tool_call_arguments
Context Passing:
server_contexthash passed through tool/prompt calls for request-specific data- Methods can accept
server_context:keyword argument for accessing context
Integration patterns
- Rails controllers: Use
server.handle_json(request.body.read)for HTTP endpoints - Command-line tools: Use
StdioTransport.new(server).openfor CLI applications - HTTP services: Use
StreamableHttpTransportfor web-based servers