Class: MCPClient::ServerBase

Inherits:
Object
  • Object
show all
Defined in:
lib/mcp_client/server_base.rb

Overview

Base class for MCP servers - serves as the interface for different server implementations

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil) ⇒ ServerBase

Initialize the server with a name

Parameters:

  • name (String, nil) (defaults to: nil)

    server name



12
13
14
# File 'lib/mcp_client/server_base.rb', line 12

def initialize(name: nil)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/mcp_client/server_base.rb', line 8

def name
  @name
end

Instance Method Details

#call_tool(tool_name, parameters) ⇒ Object

Call a tool with the given parameters

Parameters:

  • tool_name (String)

    the name of the tool to call

  • parameters (Hash)

    the parameters to pass to the tool

Returns:

  • (Object)

    the result of the tool invocation

Raises:

  • (NotImplementedError)


32
33
34
# File 'lib/mcp_client/server_base.rb', line 32

def call_tool(tool_name, parameters)
  raise NotImplementedError, 'Subclasses must implement call_tool'
end

#call_tool_streaming(tool_name, parameters) ⇒ Enumerator

Stream a tool call result (default implementation returns single-value stream)

Parameters:

  • tool_name (String)

    the name of the tool to call

  • parameters (Hash)

    the parameters to pass to the tool

Returns:

  • (Enumerator)

    stream of results



117
118
119
120
121
# File 'lib/mcp_client/server_base.rb', line 117

def call_tool_streaming(tool_name, parameters)
  Enumerator.new do |yielder|
    yielder << call_tool(tool_name, parameters)
  end
end

#capabilitiesHash?

Get server capabilities

Returns:

  • (Hash, nil)

    server capabilities

Raises:

  • (NotImplementedError)


87
88
89
# File 'lib/mcp_client/server_base.rb', line 87

def capabilities
  raise NotImplementedError, 'Subclasses must implement capabilities'
end

#cleanupObject

Clean up the server connection

Raises:

  • (NotImplementedError)


92
93
94
# File 'lib/mcp_client/server_base.rb', line 92

def cleanup
  raise NotImplementedError, 'Subclasses must implement cleanup'
end

#connectBoolean

Initialize a connection to the MCP server

Returns:

  • (Boolean)

    true if connection successful

Raises:

  • (NotImplementedError)


18
19
20
# File 'lib/mcp_client/server_base.rb', line 18

def connect
  raise NotImplementedError, 'Subclasses must implement connect'
end

#get_prompt(prompt_name, parameters) ⇒ Object

Get a prompt with the given parameters

Parameters:

  • prompt_name (String)

    the name of the prompt to get

  • parameters (Hash)

    the parameters to pass to the prompt

Returns:

  • (Object)

    the result of the prompt interpolation

Raises:

  • (NotImplementedError)


46
47
48
# File 'lib/mcp_client/server_base.rb', line 46

def get_prompt(prompt_name, parameters)
  raise NotImplementedError, 'Subclasses must implement get_prompt'
end

#list_promptsArray<MCPClient::Prompt>

List all prompts available from the MCP server

Returns:

Raises:

  • (NotImplementedError)


38
39
40
# File 'lib/mcp_client/server_base.rb', line 38

def list_prompts
  raise NotImplementedError, 'Subclasses must implement list_prompts'
end

#list_resource_templates(cursor: nil) ⇒ Hash

List all resource templates available from the MCP server

Parameters:

  • cursor (String, nil) (defaults to: nil)

    optional cursor for pagination

Returns:

  • (Hash)

    result containing resourceTemplates array and optional nextCursor

Raises:

  • (NotImplementedError)


67
68
69
# File 'lib/mcp_client/server_base.rb', line 67

def list_resource_templates(cursor: nil)
  raise NotImplementedError, 'Subclasses must implement list_resource_templates'
end

#list_resources(cursor: nil) ⇒ Hash

List all resources available from the MCP server

Parameters:

  • cursor (String, nil) (defaults to: nil)

    optional cursor for pagination

Returns:

  • (Hash)

    result containing resources array and optional nextCursor

Raises:

  • (NotImplementedError)


53
54
55
# File 'lib/mcp_client/server_base.rb', line 53

def list_resources(cursor: nil)
  raise NotImplementedError, 'Subclasses must implement list_resources'
end

#list_toolsArray<MCPClient::Tool>

List all tools available from the MCP server

Returns:

Raises:

  • (NotImplementedError)


24
25
26
# File 'lib/mcp_client/server_base.rb', line 24

def list_tools
  raise NotImplementedError, 'Subclasses must implement list_tools'
end

#on_notification {|method, params| ... } ⇒ void

This method returns an undefined value.

Register a callback to receive JSON-RPC notifications

Yields:

  • (method, params)

    invoked when a notification is received



132
133
134
# File 'lib/mcp_client/server_base.rb', line 132

def on_notification(&block)
  @notification_callback = block
end

#pingObject

Ping the MCP server to check connectivity (zero-parameter heartbeat call)

Returns:

  • (Object)

    result from the ping request



125
126
127
# File 'lib/mcp_client/server_base.rb', line 125

def ping
  rpc_request('ping')
end

#read_resource(uri) ⇒ Array<MCPClient::ResourceContent>

Read a resource by its URI

Parameters:

  • uri (String)

    the URI of the resource to read

Returns:

Raises:

  • (NotImplementedError)


60
61
62
# File 'lib/mcp_client/server_base.rb', line 60

def read_resource(uri)
  raise NotImplementedError, 'Subclasses must implement read_resource'
end

#rpc_notify(method, params = {}) ⇒ void

This method returns an undefined value.

Send a JSON-RPC notification (no response expected)

Parameters:

  • method (String)

    JSON-RPC method name

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

    parameters for the notification

Raises:

  • (NotImplementedError)


109
110
111
# File 'lib/mcp_client/server_base.rb', line 109

def rpc_notify(method, params = {})
  raise NotImplementedError, 'Subclasses must implement rpc_notify'
end

#rpc_request(method, params = {}) ⇒ Object

Send a JSON-RPC request and return the result

Parameters:

  • method (String)

    JSON-RPC method name

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

    parameters for the request

Returns:

  • (Object)

    result field from the JSON-RPC response

Raises:



101
102
103
# File 'lib/mcp_client/server_base.rb', line 101

def rpc_request(method, params = {})
  raise NotImplementedError, 'Subclasses must implement rpc_request'
end

#subscribe_resource(uri) ⇒ Boolean

Subscribe to resource updates

Parameters:

  • uri (String)

    the URI of the resource to subscribe to

Returns:

  • (Boolean)

    true if subscription successful

Raises:

  • (NotImplementedError)


74
75
76
# File 'lib/mcp_client/server_base.rb', line 74

def subscribe_resource(uri)
  raise NotImplementedError, 'Subclasses must implement subscribe_resource'
end

#unsubscribe_resource(uri) ⇒ Boolean

Unsubscribe from resource updates

Parameters:

  • uri (String)

    the URI of the resource to unsubscribe from

Returns:

  • (Boolean)

    true if unsubscription successful

Raises:

  • (NotImplementedError)


81
82
83
# File 'lib/mcp_client/server_base.rb', line 81

def unsubscribe_resource(uri)
  raise NotImplementedError, 'Subclasses must implement unsubscribe_resource'
end