Skip to main content

phi_core/mcp/
mod.rs

1//! MCP (Model Context Protocol) client support.
2//!
3//! Connect to MCP tool servers and use their tools seamlessly within phi-core.
4//!
5//! # Example
6//!
7//! ```rust,no_run
8//! use phi_core::mcp::McpClient;
9//!
10//! # async fn example() -> Result<(), phi_core::mcp::McpError> {
11//! // Connect to an MCP server via stdio
12//! let client = McpClient::connect_stdio("npx", &["-y", "@modelcontextprotocol/server-filesystem", "/tmp"], None).await?;
13//! # Ok(())
14//! # }
15//! ```
16/*
17ARCHITECTURE: MCP — extending agents with external tool servers
18
19The Model Context Protocol (MCP) is an open standard (from Anthropic) for connecting
20AI agents to tool servers. An MCP server is an external process that exposes tools
21over a standardized JSON-RPC 2.0 protocol.
22
23Why MCP vs built-in tools?
24  - MCP tools are EXTERNAL — they run in separate processes, can be in any language
25  - They can expose arbitrary capabilities (filesystem, databases, APIs, browsers, ...)
26  - They're reusable across different agents and frameworks
27  - Official server catalog at github.com/modelcontextprotocol/servers
28
29The MCP client in this module:
30  1. Spawns or connects to an MCP server (stdio or HTTP)
31  2. Performs the MCP initialization handshake (capability negotiation)
32  3. Lists available tools (`tools/list`)
33  4. Calls tools (`tools/call`) and returns results
34  5. Wraps each MCP tool as an `McpToolAdapter` implementing `AgentTool`
35
36This means the agent loop is completely unaware it's using MCP — it sees
37`Arc<dyn AgentTool>` objects, same as built-in tools.
38
39Module layout:
40  `types.rs`        — JSON-RPC 2.0 types + MCP protocol types
41  `transport.rs`    — `McpTransport` trait + `StdioTransport` + `HttpTransport`
42  `client.rs`       — `McpClient`: handshake + tool list + tool call
43  `tool_adapter.rs` — `McpToolAdapter`: wraps an MCP tool as `AgentTool`
44*/
45
46pub mod client;
47pub mod tool_adapter;
48pub mod transport;
49pub mod types;
50
51pub use client::{McpClient, McpClientConfig};
52pub use tool_adapter::McpToolAdapter;
53pub use transport::{HttpTransport, McpTransport, StdioTransport, DEFAULT_REQUEST_TIMEOUT};
54pub use types::{McpContent, McpError, McpToolCallResult, McpToolInfo, ServerInfo};