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 (for backward compatibility).
-
#get_prompt(name, arguments = {}) ⇒ Hash
Get a prompt by name (for backward compatibility).
-
#handle_json(json_string) ⇒ String
Handle a JSON-RPC request.
-
#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.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 26 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) # Create dynamic Resource classes from resource definitions resource_classes = create_resource_classes(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_classes, prompts: prompt_classes ) end |
Instance Attribute Details
#mcp_server ⇒ Object (readonly)
Returns the value of attribute mcp_server.
24 25 26 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 24 def mcp_server @mcp_server end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
24 25 26 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 24 def name @name end |
#prompts ⇒ Object (readonly)
Returns the value of attribute prompts.
24 25 26 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 24 def prompts @prompts end |
#resources ⇒ Object (readonly)
Returns the value of attribute resources.
24 25 26 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 24 def resources @resources end |
#tools ⇒ Object (readonly)
Returns the value of attribute tools.
24 25 26 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 24 def tools @tools end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
24 25 26 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 24 def version @version end |
Instance Method Details
#call_tool(name, arguments) ⇒ Hash
Execute a tool by name (for backward compatibility)
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 78 def call_tool(name, arguments) tool = @tools.find { |t| t.name == name } raise "Tool '#{name}' not found" unless tool # Call the tool's handler result = tool.handler.call(arguments) # Ensure result has the expected format unless result.is_a?(Hash) && result[:content] raise "Tool '#{name}' must return a hash with :content key" end result end |
#get_prompt(name, arguments = {}) ⇒ Hash
Get a prompt by name (for backward compatibility)
140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 140 def get_prompt(name, arguments = {}) prompt = @prompts.find { |p| p.name == name } raise "Prompt '#{name}' not found" unless prompt # Call the prompt's generator result = prompt.generator.call(arguments) # Ensure result has the expected format unless result.is_a?(Hash) && result[:messages] raise "Prompt '#{name}' must return a hash with :messages key" end result end |
#handle_json(json_string) ⇒ String
Handle a JSON-RPC request
55 56 57 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 55 def handle_json(json_string) @mcp_server.handle_json(json_string) end |
#list_prompts ⇒ Array<Hash>
List all available prompts (for backward compatibility)
126 127 128 129 130 131 132 133 134 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 126 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)
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 95 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)
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 61 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)
109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 109 def read_resource(uri) resource = @resources.find { |r| r.uri == uri } raise "Resource '#{uri}' not found" unless resource # Call the resource's reader content = resource.reader.call # Ensure content has the expected format unless content.is_a?(Hash) && content[:contents] raise "Resource '#{uri}' must return a hash with :contents key" end content end |