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

Direct Known Subclasses

ServerSSE, ServerStdio

Instance Method Summary collapse

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)


22
23
24
# File 'lib/mcp_client/server_base.rb', line 22

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

#cleanupObject

Clean up the server connection

Raises:

  • (NotImplementedError)


27
28
29
# File 'lib/mcp_client/server_base.rb', line 27

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)


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

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

#list_toolsArray<MCPClient::Tool>

List all tools available from the MCP server

Returns:

Raises:

  • (NotImplementedError)


14
15
16
# File 'lib/mcp_client/server_base.rb', line 14

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



58
59
60
# File 'lib/mcp_client/server_base.rb', line 58

def on_notification(&block)
  @notification_callback = block
end

#ping(params = {}) ⇒ Object

Ping the MCP server to check connectivity

Parameters:

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

    optional parameters for the ping request

Returns:

  • (Object)

    result from the ping request



51
52
53
# File 'lib/mcp_client/server_base.rb', line 51

def ping(params = {})
  rpc_request('ping', params)
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)


44
45
46
# File 'lib/mcp_client/server_base.rb', line 44

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:



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

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