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 and HTTP transports and focuses on discovering tools that can be used through LLM::Context and LLM::Agent.
Like LLM::Context, an MCP client is stateful and is expected to remain isolated to a single thread.
Defined Under Namespace
Modules: RPC, Transport Classes: Command, Error, Pipe
Constant Summary collapse
- TimeoutError =
Class.new(Error)
- @@clients =
{}
Class Method Summary collapse
- .clients ⇒ Object private
-
.http(llm = nil, **http) ⇒ LLM::MCP
Builds an MCP client that uses the HTTP transport.
-
.stdio(llm = nil, **stdio) ⇒ LLM::MCP
Builds an MCP client that uses the stdio transport.
Instance Method Summary collapse
-
#call_tool(name, arguments = {}) ⇒ Object
Calls a tool by name with the given arguments.
-
#initialize(llm = nil, stdio: nil, http: nil, timeout: 30) ⇒ LLM::MCP
constructor
A new MCP instance.
-
#persist! ⇒ LLM::MCP
Configures an HTTP MCP transport to use a persistent connection pool via the optional dependency [Net::HTTP::Persistent](github.com/drbrain/net-http-persistent).
-
#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: nil, http: nil, timeout: 30) ⇒ LLM::MCP
Returns A new MCP instance.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/llm/mcp.rb', line 71 def initialize(llm = nil, stdio: nil, http: nil, timeout: 30) @llm = llm @timeout = timeout if stdio && http raise ArgumentError, "stdio and http are mutually exclusive" elsif stdio @command = Command.new(**stdio) @transport = Transport::Stdio.new(command:) elsif http @transport = Transport::HTTP.new(**http, timeout:) else raise ArgumentError, "stdio or http is required" end end |
Class Method Details
.clients ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
29 |
# File 'lib/llm/mcp.rb', line 29 def self.clients = @@clients |
.http(llm = nil, **http) ⇒ LLM::MCP
Builds an MCP client that uses the HTTP transport.
49 50 51 |
# File 'lib/llm/mcp.rb', line 49 def self.http(llm = nil, **http) new(llm, http:) end |
.stdio(llm = nil, **stdio) ⇒ LLM::MCP
Builds an MCP client that uses the stdio transport.
38 39 40 |
# File 'lib/llm/mcp.rb', line 38 def self.stdio(llm = nil, **stdio) new(llm, stdio:) end |
Instance Method Details
#call_tool(name, arguments = {}) ⇒ Object
Calls a tool by name with the given arguments
128 129 130 131 |
# File 'lib/llm/mcp.rb', line 128 def call_tool(name, arguments = {}) res = call(transport, "tools/call", {name:, arguments:}) adapt_tool_result(res) end |
#persist! ⇒ LLM::MCP
Configures an HTTP MCP transport to use a persistent connection pool via the optional dependency [Net::HTTP::Persistent](github.com/drbrain/net-http-persistent)
110 111 112 113 |
# File 'lib/llm/mcp.rb', line 110 def persist! transport.persist! self end |
#start ⇒ void
This method returns an undefined value.
Starts the MCP process.
89 90 91 92 93 |
# File 'lib/llm/mcp.rb', line 89 def start transport.start call(transport, "initialize", {clientInfo: {name: "llm.rb", version: LLM::VERSION}}) call(transport, "notifications/initialized") end |
#stop ⇒ void
This method returns an undefined value.
Stops the MCP process.
98 99 100 101 |
# File 'lib/llm/mcp.rb', line 98 def stop transport.stop nil end |