Class: MCPClient::ServerBase
- Inherits:
-
Object
- Object
- MCPClient::ServerBase
- 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
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#call_tool(tool_name, parameters) ⇒ Object
Call a tool with the given parameters.
-
#call_tool_streaming(tool_name, parameters) ⇒ Enumerator
Stream a tool call result (default implementation returns single-value stream).
-
#capabilities ⇒ Hash?
Get server capabilities.
-
#cleanup ⇒ Object
Clean up the server connection.
-
#connect ⇒ Boolean
Initialize a connection to the MCP server.
-
#get_prompt(prompt_name, parameters) ⇒ Object
Get a prompt with the given parameters.
-
#initialize(name: nil) ⇒ ServerBase
constructor
Initialize the server with a name.
-
#list_prompts ⇒ Array<MCPClient::Prompt>
List all prompts available from the MCP server.
-
#list_resource_templates(cursor: nil) ⇒ Hash
List all resource templates available from the MCP server.
-
#list_resources(cursor: nil) ⇒ Hash
List all resources available from the MCP server.
-
#list_tools ⇒ Array<MCPClient::Tool>
List all tools available from the MCP server.
-
#on_notification {|method, params| ... } ⇒ void
Register a callback to receive JSON-RPC notifications.
-
#ping ⇒ Object
Ping the MCP server to check connectivity (zero-parameter heartbeat call).
-
#read_resource(uri) ⇒ Array<MCPClient::ResourceContent>
Read a resource by its URI.
-
#rpc_notify(method, params = {}) ⇒ void
Send a JSON-RPC notification (no response expected).
-
#rpc_request(method, params = {}) ⇒ Object
Send a JSON-RPC request and return the result.
-
#subscribe_resource(uri) ⇒ Boolean
Subscribe to resource updates.
-
#unsubscribe_resource(uri) ⇒ Boolean
Unsubscribe from resource updates.
Constructor Details
#initialize(name: nil) ⇒ ServerBase
Initialize the server with a name
12 13 14 |
# File 'lib/mcp_client/server_base.rb', line 12 def initialize(name: nil) @name = name end |
Instance Attribute Details
#name ⇒ Object (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
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)
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 |
#capabilities ⇒ Hash?
Get server capabilities
87 88 89 |
# File 'lib/mcp_client/server_base.rb', line 87 def capabilities raise NotImplementedError, 'Subclasses must implement capabilities' end |
#cleanup ⇒ Object
Clean up the server connection
92 93 94 |
# File 'lib/mcp_client/server_base.rb', line 92 def cleanup raise NotImplementedError, 'Subclasses must implement cleanup' end |
#connect ⇒ Boolean
Initialize a connection to the MCP server
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
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_prompts ⇒ Array<MCPClient::Prompt>
List all prompts available from the MCP server
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
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
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_tools ⇒ Array<MCPClient::Tool>
List all tools available from the MCP server
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
132 133 134 |
# File 'lib/mcp_client/server_base.rb', line 132 def on_notification(&block) @notification_callback = block end |
#ping ⇒ Object
Ping the MCP server to check connectivity (zero-parameter heartbeat call)
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
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)
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
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
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
81 82 83 |
# File 'lib/mcp_client/server_base.rb', line 81 def unsubscribe_resource(uri) raise NotImplementedError, 'Subclasses must implement unsubscribe_resource' end |