Skip to content

Add Model Context Protocol (MCP) client support #35

Open

Description

@evansenter

Summary

Implement client-side support for Model Context Protocol, the open standard for LLM-tool integration.

Why MCP

MCP has emerged as the industry standard with adoption from OpenAI, Microsoft, Google, AWS, and Bloomberg. It was donated by Anthropic to the Agentic AI Foundation (Linux Foundation).

Benefits:

  • Interoperability with entire MCP ecosystem
  • Users can leverage any MCP server (databases, APIs, filesystems)
  • 97M+ monthly SDK downloads

Relationship with ToolRegistry (#84)

MCP tools should integrate with the ToolRegistry from #84:

// MCP client wraps ToolRegistry
let mcp_client = McpClient::builder()
    .add_server("filesystem", "npx @modelcontextprotocol/server-filesystem")
    .build()
    .await?;

// MCP tools get wrapped to implement our Tool trait
let mcp_tools: Vec<Box<dyn Tool>> = mcp_client.discover_tools().await?;

// Register MCP tools alongside native tools
let registry = ToolRegistry::new()
    .register(Calculator::new())        // Native tool
    .register_all(mcp_tools);           // MCP tools

// Agents use both seamlessly via ToolSet
let agent = ReactAgent::new(ReactConfig {
    tools: ToolSet::All,  // Uses both native and MCP tools
    ..Default::default()
})?;

MCP Tool Wrapper

/// Wraps an MCP tool to implement our Tool trait
struct McpToolWrapper {
    server: Arc<McpServer>,
    name: String,
    description: String,
    schema: serde_json::Value,
}

impl Tool for McpToolWrapper {
    fn name(&self) -> &str { &self.name }
    fn description(&self) -> &str { &self.description }
    fn parameters_schema(&self) -> serde_json::Value { self.schema.clone() }
    
    async fn execute(&self, input: serde_json::Value) -> Result<ToolOutput, ToolError> {
        self.server.call_tool(&self.name, input).await
    }
}

Proposed API

// Connect to MCP servers
let mcp_client = McpClient::builder()
    .add_server("filesystem", McpServerConfig::stdio("npx", &["@modelcontextprotocol/server-filesystem"]))
    .add_server("github", McpServerConfig::stdio("mcp-server-github", &[]))
    .build()
    .await?;

// Discover available tools (returns Vec<Box<dyn Tool>>)
let tools = mcp_client.discover_tools().await?;

// Or integrate directly with registry
let registry = ToolRegistry::new()
    .with_mcp_client(&mcp_client)
    .await?;

Transport Support

Phase 1: stdio (Priority)

Most common transport for local MCP servers:

pub enum McpTransport {
    Stdio {
        command: String,
        args: Vec<String>,
        env: HashMap<String, String>,
    },
}

Phase 2: HTTP/SSE (Future)

For remote MCP servers:

pub enum McpTransport {
    Stdio { /* ... */ },
    Http {
        url: String,
        headers: HashMap<String, String>,
    },
    Sse {
        url: String,
        headers: HashMap<String, String>,
    },
}

Extensibility

Use trait-based transport for future extensibility:

#[async_trait]
pub trait McpTransport: Send + Sync {
    async fn connect(&mut self) -> Result<(), McpError>;
    async fn send(&self, request: JsonRpcRequest) -> Result<JsonRpcResponse, McpError>;
    async fn close(&mut self) -> Result<(), McpError>;
}

// Built-in implementations
pub struct StdioTransport { /* ... */ }
pub struct HttpTransport { /* ... */ }  // Future
pub struct SseTransport { /* ... */ }   // Future

// Users can implement custom transports
impl McpTransport for MyCustomTransport { /* ... */ }

Technical Requirements

  • JSON-RPC 2.0 client implementation
  • stdio transport (spawn subprocess, communicate via stdin/stdout)
  • Tool discovery via tools/list method
  • Tool execution via tools/call method
  • Graceful server lifecycle management (start/stop)
  • Async operation handling (per 2025-11-25 spec)

Implementation Notes

Priority

Low - Implement native tools (#84) first. MCP provides ecosystem access but native tools cover core use cases.

References

Related Issues

Activity

  1. added
    core
    enhancement
    priority:medium
    on Dec 26, 2025
  2. evansenter commented on Dec 26, 2025

    @evansenter
    OwnerAuthor

    Implementation Notes (from codebase audit)

    Integration approach with existing architecture:

    1. MCP tools as Agent tools - MCP servers provide tools that can be used by ReAct (Implement ReAct agent with configurable tools #40) and other tool-using agents:

      pub struct McpTool {
          server: McpServer,
          tool_schema: ToolSchema,
      }
      
      impl Tool for McpTool {
          async fn execute(&self, input: Value) -> Result<Value, ToolError> {
              self.server.call_tool(&self.tool_schema.name, input).await
          }
      }
    2. LlmClient already supports structured output - can use for tool call parsing in gemicro-core/src/llm.rs

    3. Suggested crate structure:

      gemicro-mcp/
      ├── Cargo.toml
      └── src/
          ├── lib.rs
          ├── client.rs      # McpClient, server connections
          ├── transport.rs   # stdio, HTTP+SSE
          ├── protocol.rs    # JSON-RPC 2.0
          └── tools.rs       # Tool discovery, schema validation
      
    4. Add to workspace dependencies:

      [workspace.dependencies]
      gemicro-mcp = { path = "gemicro-mcp" }

    Rust MCP resources:

    Dependencies consideration:

  3. evansenter commented on Dec 26, 2025

    @evansenter
    OwnerAuthor

    Note: MCP integration would be especially valuable for the ReAct agent, which already has a tool-use architecture. MCP tools could be registered alongside built-in tools (calculator) to dramatically expand the agent's capabilities.

  4. added
    priority:low
    priority:medium
    and removed
    priority:medium
    priority:low
    on Dec 27, 2025
  5. m13v commented on Mar 18, 2026

    @m13v

    MCP client support is a great addition. we built both an MCP server and client for our macOS agent and the main pain point was handling the different transport layers (stdio vs SSE) cleanly. having a pluggable transport interface from the start saves a lot of refactoring later. also worth implementing tool discovery caching so you don't re-fetch the tool list on every request.

  6. m13v commented on Mar 18, 2026

    @m13v

    here's our MCP server implementation that handles tool discovery and transport: https://github.com/mediar-ai/terminator/blob/main/crates/terminator-mcp-agent/src/server.rs

    and the client-side provider that manages connections to multiple MCP backends: https://github.com/m13v/fazm/blob/main/Desktop/Sources/Providers/ChatProvider.swift

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    coreenhancementpriority:medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions