Class: ClaudeAgentSDK::SdkMcpServer
- Inherits:
-
Object
- Object
- ClaudeAgentSDK::SdkMcpServer
- Defined in:
- lib/claude_agent_sdk/sdk_mcp_server.rb
Overview
SDK MCP Server - wraps official MCP::Server with block-based API
Unlike external MCP servers that run as separate processes, SDK MCP servers run directly in your application's process, providing better performance and simpler deployment.
This class wraps the official MCP Ruby SDK and provides a simpler block-based API for defining tools, resources, and prompts.
Instance Attribute Summary collapse
-
#mcp_server ⇒ Object
readonly
Returns the value of attribute mcp_server.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#prompts ⇒ Object
readonly
Returns the value of attribute prompts.
-
#resources ⇒ Object
readonly
Returns the value of attribute resources.
-
#tools ⇒ Object
readonly
Returns the value of attribute tools.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
-
#call_tool(name, arguments) ⇒ Hash
Execute a tool by name (backward-compat public API; Query's tools/call dispatch routes through handle_message/the official MCP::Server, which also validates arguments against the tool's inputSchema — this direct path bypasses that validation).
-
#get_prompt(name, arguments = {}) ⇒ Hash
Get a prompt by name (for backward compatibility).
-
#handle_json(json_string) ⇒ String
Handle a JSON-RPC request.
-
#handle_message(message) ⇒ Hash
Route one JSON-RPC request hash (symbol keys, as produced by the transport) through the official MCP::Server.
-
#initialize(name:, version: '1.0.0', tools: [], resources: [], prompts: []) ⇒ SdkMcpServer
constructor
A new instance of SdkMcpServer.
-
#list_prompts ⇒ Array<Hash>
List all available prompts (for backward compatibility).
-
#list_resources ⇒ Array<Hash>
List all available resources (for backward compatibility).
-
#list_tools ⇒ Array<Hash>
List all available tools (for backward compatibility).
-
#read_resource(uri) ⇒ Hash
Read a resource by URI (for backward compatibility).
Constructor Details
#initialize(name:, version: '1.0.0', tools: [], resources: [], prompts: []) ⇒ SdkMcpServer
Returns a new instance of SdkMcpServer.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 84 def initialize(name:, version: '1.0.0', tools: [], resources: [], prompts: []) @name = name @version = version @tools = tools @resources = resources @prompts = prompts # Create dynamic Tool classes from tool definitions tool_classes = create_tool_classes(tools) # Resources are served as MCP::Resource instances; reads go through # the gem's registerable handler (see register_resources_read_handler). resource_instances = create_resource_instances(resources) # Create dynamic Prompt classes from prompt definitions prompt_classes = create_prompt_classes(prompts) # Create the official MCP::Server instance @mcp_server = MCP::Server.new( name: name, version: version, tools: tool_classes, resources: resource_instances, prompts: prompt_classes ) register_resources_read_handler end |
Instance Attribute Details
#mcp_server ⇒ Object (readonly)
Returns the value of attribute mcp_server.
82 83 84 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 82 def mcp_server @mcp_server end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
82 83 84 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 82 def name @name end |
#prompts ⇒ Object (readonly)
Returns the value of attribute prompts.
82 83 84 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 82 def prompts @prompts end |
#resources ⇒ Object (readonly)
Returns the value of attribute resources.
82 83 84 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 82 def resources @resources end |
#tools ⇒ Object (readonly)
Returns the value of attribute tools.
82 83 84 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 82 def tools @tools end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
82 83 84 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 82 def version @version end |
Instance Method Details
#call_tool(name, arguments) ⇒ Hash
Execute a tool by name (backward-compat public API; Query's tools/call dispatch routes through handle_message/the official MCP::Server, which also validates arguments against the tool's inputSchema — this direct path bypasses that validation). Tool-execution failures are reported in-band (isError: true) per the MCP spec and Python parity (the mcp lowlevel server converts handler exceptions to CallToolResult(isError=True)); they must NOT become JSON-RPC protocol errors — the model needs the error text to self-correct.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 170 def call_tool(name, arguments) tool = @tools.find { |t| t.name == name } return error_tool_result("Tool '#{name}' not found") unless tool # Call the tool's handler on a plain thread so the async gem's # Fiber scheduler is not visible to user code (which may hit AR/PG). result = FiberBoundary.invoke { tool.handler.call(arguments) } # Guard before flexible_fetch: it raises on non-Hash inputs. content = result.is_a?(Hash) ? ClaudeAgentSDK.flexible_fetch(result, "content", "content") : nil return error_tool_result("Tool '#{name}' must return a hash with :content key") unless content result rescue StandardError => e # Bare e.message like Python's str(e) — no prefix. error_tool_result(e.) end |
#get_prompt(name, arguments = {}) ⇒ Hash
Get a prompt by name (for backward compatibility)
237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 237 def get_prompt(name, arguments = {}) prompt = @prompts.find { |p| p.name == name } raise "Prompt '#{name}' not found" unless prompt # Hop off the Fiber scheduler before invoking user code — same reason # as `call_tool` above. result = FiberBoundary.invoke { prompt.generator.call(arguments) } # Ensure result has the expected format (symbol or string keys) = result.is_a?(Hash) ? ClaudeAgentSDK.flexible_fetch(result, "messages", "messages") : nil raise "Prompt '#{name}' must return a hash with :messages key" if .nil? result end |
#handle_json(json_string) ⇒ String
Handle a JSON-RPC request
115 116 117 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 115 def handle_json(json_string) @mcp_server.handle_json(json_string) end |
#handle_message(message) ⇒ Hash
Route one JSON-RPC request hash (symbol keys, as produced by the transport) through the official MCP::Server. Two sanitations, both empirically required:
- The gem's JsonRpcHandler rejects string ids not matching /\A[a-zA-Z0-9_-]+\z/ with nil, error: -32600 (Python echoes any id verbatim) — swap in a safe id and re-stamp the original on the response (error envelopes too).
- The gem rejects messages lacking jsonrpc: '2.0' with -32600; Python never inspects this field and the CLI's embedded mcp_message shape is not guaranteed — force it. NOTE on concurrency: Query runs each control_request in its own async task, so two tools/call can interleave inside the gem's Server#handle. Responses are built from per-call locals (safe), but the gem's instrumentation_callback attribution (@instrumentation_data ivar) can cross-contaminate under concurrency — harmless with the default no-op.
136 137 138 139 140 141 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 136 def () original_id = [:id] response = @mcp_server.handle(.merge(jsonrpc: '2.0', id: 0)) response[:id] = original_id if response.is_a?(Hash) && response.key?(:id) normalize_tools_call_errors(, response) end |
#list_prompts ⇒ Array<Hash>
List all available prompts (for backward compatibility)
223 224 225 226 227 228 229 230 231 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 223 def list_prompts @prompts.map do |prompt| { name: prompt.name, description: prompt.description, arguments: prompt.arguments }.compact end end |
#list_resources ⇒ Array<Hash>
List all available resources (for backward compatibility)
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 190 def list_resources @resources.map do |resource| { uri: resource.uri, name: resource.name, description: resource.description, mimeType: resource.mime_type }.compact end end |
#list_tools ⇒ Array<Hash>
List all available tools (for backward compatibility)
145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 145 def list_tools @tools.map do |tool| entry = { name: tool.name, description: tool.description, inputSchema: convert_input_schema(tool.input_schema) } entry[:annotations] = tool.annotations if tool.annotations entry[:_meta] = tool. if tool. entry end end |
#read_resource(uri) ⇒ Hash
Read a resource by URI (for backward compatibility)
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 204 def read_resource(uri) resource = @resources.find { |r| r.uri == uri } raise "Resource '#{uri}' not found" unless resource # Hop off the Fiber scheduler before invoking user code — same reason # as `call_tool` above: reader blocks may touch Thread.current-keyed # libraries (ActiveRecord, pg, ...) and must run on a plain thread. content = FiberBoundary.invoke { resource.reader.call } # Ensure content has the expected format (symbol or string keys; guard # before flexible_fetch — it raises on non-Hash inputs) contents = content.is_a?(Hash) ? ClaudeAgentSDK.flexible_fetch(content, "contents", "contents") : nil raise "Resource '#{uri}' must return a hash with :contents key" if contents.nil? content end |