Class: ClaudeAgentSDK::SdkMcpServer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_serverObject (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

#nameObject (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

#promptsObject (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

#resourcesObject (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

#toolsObject (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

#versionObject (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)

Parameters:

  • name (String)

    Tool name

  • arguments (Hash)

    Tool arguments

Returns:

  • (Hash)

    Tool result



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 66

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)

Parameters:

  • name (String)

    Prompt name

  • arguments (Hash) (defaults to: {})

    Arguments to fill in the prompt template

Returns:

  • (Hash)

    Prompt with filled-in arguments



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 128

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

Parameters:

  • json_string (String)

    JSON-RPC request

Returns:

  • (String)

    JSON-RPC response



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_promptsArray<Hash>

List all available prompts (for backward compatibility)

Returns:

  • (Array<Hash>)

    Array of prompt definitions



114
115
116
117
118
119
120
121
122
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 114

def list_prompts
  @prompts.map do |prompt|
    {
      name: prompt.name,
      description: prompt.description,
      arguments: prompt.arguments
    }.compact
  end
end

#list_resourcesArray<Hash>

List all available resources (for backward compatibility)

Returns:

  • (Array<Hash>)

    Array of resource definitions



83
84
85
86
87
88
89
90
91
92
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 83

def list_resources
  @resources.map do |resource|
    {
      uri: resource.uri,
      name: resource.name,
      description: resource.description,
      mimeType: resource.mime_type
    }.compact
  end
end

#list_toolsArray<Hash>

List all available tools (for backward compatibility)

Returns:

  • (Array<Hash>)

    Array of tool definitions



52
53
54
55
56
57
58
59
60
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 52

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 (for backward compatibility)

Parameters:

  • uri (String)

    Resource URI

Returns:

  • (Hash)

    Resource content



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/claude_agent_sdk/sdk_mcp_server.rb', line 97

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