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



62
63
64
65
66
# File 'lib/mcp_client/server_base.rb', line 62

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

#cleanupObject

Clean up the server connection

Raises:

  • (NotImplementedError)


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

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

#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



77
78
79
# File 'lib/mcp_client/server_base.rb', line 77

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



70
71
72
# File 'lib/mcp_client/server_base.rb', line 70

def ping
  rpc_request('ping')
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)


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

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:



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

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