cortex-mcp
Model Context Protocol (MCP) support for cortex.
Overview
This crate provides both client and server implementations of the Model Context Protocol, enabling:
- Client: Connect to MCP servers and use their tools in your agents
- Server: Expose your tools and agents as MCP servers for use with Claude Desktop, VS Code, etc.
Features
- Full MCP protocol implementation (JSON-RPC 2.0)
- STDIO and SSE transports
- Tool, Resource, and Prompt support
- Easy tool bridging between MCP and cortex
Quick Start
Client: Connect to an MCP Server
use ;
// Connect to an MCP filesystem server
let transport = spawn.await?;
let mut client = new.await?;
// List available tools
let tools = client.list_tools.await?;
// Call a tool
let result = client.call_tool.await?;
Server: Create an MCP Server
use ;
;
// Build and run the server
let server = builder
.name
.version
.add_tool
.build;
server.run_stdio.await?;
Server: HTTP/SSE Transport
For web-based clients, use the SSE transport:
use ;
let server = builder
.name
.version
.add_tool
.build;
// Configure SSE server
let config = SseServerConfig ;
// Run HTTP server (blocks until shutdown)
server.run_sse.await?;
Endpoints:
GET /sse- SSE stream for server-to-client messagesPOST /message?sessionId=xxx- JSON-RPC requests from client
Using with Claude Desktop
- Build your MCP server:
- Add to Claude Desktop config (
~/Library/Application Support/Claude/claude_desktop_config.jsonon macOS):
- Restart Claude Desktop - your tools will be available!
Architecture
┌─────────────────────────────────────────────────────────────┐
│ MCP Crate │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ McpClient │ │ McpServer │ │
│ │ │ │ │ │
│ │ - list_tools() │ │ - add_tool() │ │
│ │ - call_tool() │ │ - run_stdio() │ │
│ │ - list_resources│ │ - handle_*() │ │
│ └────────┬────────┘ └────────┬────────┘ │
│ │ │ │
│ ┌────────┴────────────────────────────┴────────┐ │
│ │ McpTransport Trait │ │
│ ├──────────────────┬───────────────────────────┤ │
│ │ StdioTransport │ SseTransport │ │
│ │ (subprocess) │ (HTTP/SSE) │ │
│ └──────────────────┴───────────────────────────┘ │
│ │
│ ┌─────────────────────────────────────────────┐ │
│ │ McpToolBridge │ │
│ │ (Convert MCP tools to cortex Tool) │ │
│ └─────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
Modules
client
MCP client for connecting to external servers.
server
MCP server for exposing tools to clients.
transport
Transport implementations (STDIO, SSE).
bridge
Bridge MCP tools to cortex Tool trait.
protocol
MCP protocol types (JSON-RPC, capabilities, tool definitions).
Tool Handler Trait
Implement ToolHandler to create MCP tools:
Function-Based Tools
For simple tools, use FnTool or AsyncFnTool:
use FnTool;
let add_tool = new;
Resource and Prompt Handlers
For resources and prompts, implement the respective traits:
Examples
See the examples directory:
examples/mcp_server.rs- MCP server with STDIO transport (for Claude Desktop)examples/mcp_sse_server.rs- MCP server with HTTP/SSE transport (for web clients)examples/mcp_integration.rs- Client connecting to external MCP servers
Run examples:
# STDIO server example (for Claude Desktop)
# SSE server example (HTTP on port 3000)
# Client example (requires Node.js/npx)
Protocol Version
This implementation supports MCP protocol version 2024-11-05.
License
Apache-2.0