class

MCP::Client

Inherits Reference < Object

Main client class for communicating with MCP servers.

client = MCP::Client.new("npx", ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/dir"])
client.connect
tools = client.list_tools
result = client.call_tool("read_file", {"path" => JSON::Any.new("/path/to/file.txt")})
client.disconnect

Constructors

new(transport : Transport, client_info : ClientInfo)

Create a client with a custom transport.

Source
new(command : String, args : Array(String) = [] of String, client_name : String = "mcp-crystal-client", client_version : String = MCP::VERSION)

Create a client with STDIO transport (convenience constructor).

Source

Instance methods

call_tool(name : String, arguments : Hash(String, JSON::Any)) : ToolCallResult

Call a tool with the given arguments.

Arguments should be a Hash with JSON::Any values.

Raises MCPToolError if the tool call fails.

Source
connect

Connect to the server and perform initialization handshake.

Returns the server's information.

Source
connected?

Check if connected to the server.

Source
disconnect

Disconnect from the server.

Source
list_tools

List available tools from the server.

Raises MCPProtocolError if the server doesn't support tools.

Source
ping

Send a ping to the server to check connectivity.

Returns true if the server responds successfully.

Source
server_capabilities

Get the server's capabilities.

Returns nil if not connected or capabilities not yet negotiated.

Source
supports_logging?
Source
supports_prompts?
Source
supports_resources?
Source
supports_tools?

Check if the server supports a specific capability.

Source