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.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 17 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.
15 16 17 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 15 def mcp_server @mcp_server end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
15 16 17 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 15 def name @name end |
#prompts ⇒ Object (readonly)
Returns the value of attribute prompts.
15 16 17 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 15 def prompts @prompts end |
#resources ⇒ Object (readonly)
Returns the value of attribute resources.
15 16 17 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 15 def resources @resources end |
#tools ⇒ Object (readonly)
Returns the value of attribute tools.
15 16 17 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 15 def tools @tools end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
15 16 17 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 15 def version @version end |
Instance Method Details
#call_tool(name, arguments) ⇒ Hash
Execute a tool by name (for backward compatibility)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 68 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)
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 130 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
46 47 48 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 46 def handle_json(json_string) @mcp_server.handle_json(json_string) end |
#list_prompts ⇒ Array<Hash>
List all available prompts (for backward compatibility)
116 117 118 119 120 121 122 123 124 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 116 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)
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 85 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)
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 52 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 end end |
#read_resource(uri) ⇒ Hash
Read a resource by URI (for backward compatibility)
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 99 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 |