|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Development Commands |
| 6 | + |
| 7 | +### Core Commands |
| 8 | +- `bundle install` - Install dependencies |
| 9 | +- `rake test` - Run all tests |
| 10 | +- `rake rubocop` - Run linter |
| 11 | +- `rake` - Run tests and linting (default task) |
| 12 | + |
| 13 | +### Testing |
| 14 | +- `ruby -I lib -I test test/path/to/specific_test.rb` - Run single test file |
| 15 | +- Test files are located in `test/` directory with `_test.rb` suffix |
| 16 | + |
| 17 | +### Gem Management |
| 18 | +- `gem build mcp.gemspec` - Build the gem |
| 19 | +- Releases are triggered by updating version in `lib/mcp/version.rb` and merging to main |
| 20 | + |
| 21 | +## Architecture Overview |
| 22 | + |
| 23 | +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. |
| 24 | + |
| 25 | +### Core Components |
| 26 | + |
| 27 | +**MCP::Server** (`lib/mcp/server.rb`): |
| 28 | +- Main server class handling JSON-RPC requests |
| 29 | +- Implements MCP protocol methods: initialize, ping, tools/list, tools/call, prompts/list, prompts/get, resources/list, resources/read |
| 30 | +- Supports custom method registration via `define_custom_method` |
| 31 | +- Handles instrumentation, exception reporting, and notifications |
| 32 | +- Uses JsonRpcHandler for request processing |
| 33 | + |
| 34 | +**MCP::Client** (`lib/mcp/client.rb`): |
| 35 | +- Client interface for communicating with MCP servers |
| 36 | +- Transport-agnostic design with pluggable transport layers |
| 37 | +- Supports tool listing and invocation |
| 38 | + |
| 39 | +**Transport Layer**: |
| 40 | +- `MCP::Server::Transports::StdioTransport` - Command-line stdio transport |
| 41 | +- `MCP::Server::Transports::StreamableHttpTransport` - HTTP with streaming support |
| 42 | +- `MCP::Client::HTTP` - HTTP client transport (requires faraday gem) |
| 43 | + |
| 44 | +**Protocol Components**: |
| 45 | +- `MCP::Tool` - Tool definition with input/output schemas and annotations |
| 46 | +- `MCP::Prompt` - Prompt templates with argument validation |
| 47 | +- `MCP::Resource` - Resource registration and retrieval |
| 48 | +- `MCP::Configuration` - Global configuration with exception reporting and instrumentation |
| 49 | + |
| 50 | +### Key Patterns |
| 51 | + |
| 52 | +**Three Ways to Define Components**: |
| 53 | +1. Class inheritance (e.g., `class MyTool < MCP::Tool`) |
| 54 | +2. Define methods (e.g., `MCP::Tool.define(name: "my_tool") { ... }`) |
| 55 | +3. Server registration (e.g., `server.define_tool(name: "my_tool") { ... }`) |
| 56 | + |
| 57 | +**Schema Validation**: |
| 58 | +- Tools support input_schema and output_schema for JSON Schema validation |
| 59 | +- Protocol version 2025-03-26+ supports tool annotations (destructive_hint, idempotent_hint, etc.) |
| 60 | +- Validation is configurable via `configuration.validate_tool_call_arguments` |
| 61 | + |
| 62 | +**Context Passing**: |
| 63 | +- `server_context` hash passed through tool/prompt calls for request-specific data |
| 64 | +- Methods can accept `server_context:` keyword argument for accessing context |
| 65 | + |
| 66 | +### Dependencies |
| 67 | +- `json_rpc_handler` ~> 0.1 - JSON-RPC 2.0 message handling |
| 68 | +- `json-schema` >= 4.1 - Schema validation |
| 69 | +- Ruby 3.2.0+ required |
| 70 | + |
| 71 | +### Example Integration Patterns |
| 72 | +- Rails controllers: Use `server.handle_json(request.body.read)` for HTTP endpoints |
| 73 | +- Command-line tools: Use `StdioTransport.new(server).open` for CLI applications |
| 74 | +- HTTP services: Use `StreamableHttpTransport` for web-based servers |
0 commit comments