Class: ClaudeAgentSDK::SdkMcpServer
- Inherits:
-
Object
- Object
- ClaudeAgentSDK::SdkMcpServer
- Defined in:
- lib/claude_agent_sdk/sdk_mcp_server.rb
Overview
SDK MCP Server - runs in-process within your Ruby application
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.
Supports:
-
Tools: Executable functions that Claude can call
-
Resources: Data sources that can be read (files, databases, APIs, etc.)
-
Prompts: Reusable prompt templates with arguments
Instance Attribute Summary collapse
-
#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.
-
#get_prompt(name, arguments = {}) ⇒ Hash
Get a prompt by name.
-
#initialize(name:, version: '1.0.0', tools: [], resources: [], prompts: []) ⇒ SdkMcpServer
constructor
A new instance of SdkMcpServer.
-
#list_prompts ⇒ Array<Hash>
List all available prompts.
-
#list_resources ⇒ Array<Hash>
List all available resources.
-
#list_tools ⇒ Array<Hash>
List all available tools.
-
#read_resource(uri) ⇒ Hash
Read a resource by URI.
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 |
# 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 @tool_map = tools.each_with_object({}) { |tool, hash| hash[tool.name] = tool } @resource_map = resources.each_with_object({}) { |res, hash| hash[res.uri] = res } @prompt_map = prompts.each_with_object({}) { |prompt, hash| hash[prompt.name] = prompt } end |
Instance Attribute Details
#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
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 44 def call_tool(name, arguments) tool = @tool_map[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
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 106 def get_prompt(name, arguments = {}) prompt = @prompt_map[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 |
#list_prompts ⇒ Array<Hash>
List all available prompts
92 93 94 95 96 97 98 99 100 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 92 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
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 61 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
30 31 32 33 34 35 36 37 38 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 30 def list_tools @tools.map do |tool| { name: tool.name, description: tool.description, inputSchema: convert_input_schema(tool.input_schema) } end end |
#read_resource(uri) ⇒ Hash
Read a resource by URI
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 75 def read_resource(uri) resource = @resource_map[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 |