Class: LLM::MCP
- Inherits:
-
Object
- Object
- LLM::MCP
- Includes:
- RPC
- Defined in:
- lib/llm/mcp.rb,
lib/llm/mcp/rpc.rb,
lib/llm/mcp/pipe.rb,
lib/llm/mcp/error.rb,
lib/llm/mcp/command.rb
Overview
The LLM::MCP class provides access to servers that implement the Model Context Protocol. MCP defines a standard way for clients and servers to exchange capabilities such as tools, prompts, resources, and other structured interactions.
In llm.rb, LLM::MCP currently supports stdio servers and focuses on discovering tools that can be used through LLM::Context and LLM::Agent.
Defined Under Namespace
Modules: RPC, Transport Classes: Command, Error, Pipe
Constant Summary collapse
- TimeoutError =
Class.new(Error)
Instance Method Summary collapse
-
#call_tool(name, arguments = {}) ⇒ Object
Calls a tool by name with the given arguments.
-
#initialize(llm = nil, stdio:, timeout: 30) ⇒ LLM::MCP
constructor
A new MCP instance.
-
#start ⇒ void
Starts the MCP process.
-
#stop ⇒ void
Stops the MCP process.
-
#tools ⇒ Array<Class<LLM::Tool>>
Returns the tools provided by the MCP process.
Methods included from RPC
Constructor Details
#initialize(llm = nil, stdio:, timeout: 30) ⇒ LLM::MCP
Returns A new MCP instance.
34 35 36 37 38 39 40 |
# File 'lib/llm/mcp.rb', line 34 def initialize(llm = nil, stdio:, timeout: 30) @llm = llm @command = Command.new(**stdio) @monitor = Monitor.new @transport = Transport::Stdio.new(command:) @timeout = timeout end |
Instance Method Details
#call_tool(name, arguments = {}) ⇒ Object
Calls a tool by name with the given arguments
78 79 80 81 82 83 |
# File 'lib/llm/mcp.rb', line 78 def call_tool(name, arguments = {}) lock do res = call(transport, "tools/call", {name:, arguments:}) adapt_tool_result(res) end end |
#start ⇒ void
This method returns an undefined value.
Starts the MCP process.
45 46 47 48 49 50 51 |
# File 'lib/llm/mcp.rb', line 45 def start lock do transport.start call(transport, "initialize", {clientInfo: {name: "llm.rb", version: LLM::VERSION}}) call(transport, "notifications/initialized") end end |
#stop ⇒ void
This method returns an undefined value.
Stops the MCP process.
56 57 58 59 60 61 |
# File 'lib/llm/mcp.rb', line 56 def stop lock do transport.stop nil end end |