MCP Integration Architecture
This document describes the architecture of MCP (Model Context Protocol) integration in Radium.
Overviewβ
MCP integration allows Radium to connect to external MCP servers, discover their tools and prompts, and make them available to agents and users through the orchestration system and CLI/TUI interfaces.
Component Architectureβ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Application Layer β
β - CLI Commands (rad mcp *) β
β - TUI Chat Interface β
β - Agent Execution β
ββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β McpIntegration β
β - Server connection management β
β - Tool discovery and registry β
β - Prompt discovery and slash command registration β
β - Configuration loading (workspace + extensions) β
ββββββββββββ¬ββββββββββββββββββββββββββββββββ¬βββββββββββββββββββ
β β
βΌ βΌ
ββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββ
β McpClient β β SlashCommandRegistry β
β - Transport layer β β - Prompt-to-command mappingβ
β - JSON-RPC protocol β β - Server association β
β - OAuth integration β ββββββββββββββββββββββββββββββββ
ββββββββββββ¬ββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Transport Layer β
β ββββββββββββ ββββββββββββ ββββββββββββ β
β β Stdio β β SSE β β HTTP β β
β βTransport β βTransport β βTransport β β
β ββββββββββββ ββββββββββββ ββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Data Flowβ
Tool Discovery Flowβ
1. McpIntegration::initialize()
β
2. Load configs (workspace + extensions)
β
3. For each server:
- McpClient::connect() β Transport::connect()
- McpClient::discover_tools() β JSON-RPC "tools/list"
- McpToolRegistry::register_tool() β Conflict resolution
β
4. Tools available via McpIntegration::get_all_tools()
Tool Execution Flowβ
1. Agent requests tool execution
β
2. Orchestrator β McpIntegration::execute_tool()
β
3. McpClient::execute_tool() β JSON-RPC "tools/call"
β
4. ContentHandler::parse_content() β McpContent enum
β
5. Result returned to agent
Prompt/Slash Command Flowβ
1. McpIntegration::initialize()
β
2. McpClient::list_prompts() β JSON-RPC "prompts/list"
β
3. SlashCommandRegistry::register_prompt_with_server()
β
4. Commands available in chat: /prompt-name
β
5. User types /command β SlashCommandRegistry lookup
β
6. McpClient::execute_prompt() β JSON-RPC "prompts/get"
β
7. Result displayed
Key Componentsβ
McpIntegrationβ
Central manager for MCP server connections:
- Manages multiple server connections
- Coordinates tool and prompt discovery
- Handles configuration precedence (workspace > extension)
- Thread-safe with
Arc<Mutex<>>
McpClientβ
Per-server connection handler:
- Manages transport layer
- Handles JSON-RPC protocol
- Integrates OAuth token management
- Discovers tools and prompts
McpToolRegistryβ
Per-server tool storage:
- Stores discovered tools
- Handles name conflict resolution
- Supports dual-lookup (original + prefixed names)
SlashCommandRegistryβ
Prompt-to-command mapping:
- Maps prompts to slash commands
- Tracks server associations
- Used by chat interfaces
Transport Layerβ
Three transport implementations:
- StdioTransport: Local process communication
- SseTransport: Server-Sent Events streaming
- HttpTransport: HTTP request/response
Configuration Precedenceβ
- Workspace config (
.radium/mcp-servers.toml) - Highest precedence - Extension configs (from installed extensions) - Lower precedence
If a server name exists in both, workspace config takes precedence.
Thread Safetyβ
All shared state uses Arc<Mutex<>>:
McpIntegration::clients- Thread-safe client storageMcpIntegration::tool_registries- Thread-safe registry accessMcpIntegration::slash_registry- Thread-safe command registry
Error Handlingβ
- Connection failures are logged but don't block other servers
- Tool execution errors are propagated to agents
- OAuth token refresh happens automatically before requests
- Failed servers are skipped during initialization
Integration Pointsβ
Orchestrator Bridgeβ
crates/radium-orchestrator/src/orchestration/mcp_tools.rs:
- Converts MCP tools to orchestration Tool objects
- Handles rich content (saves images/audio to temp files)
- Provides agent-facing API
CLI Integrationβ
apps/cli/src/commands/mcp.rs:
rad mcp list- List configured serversrad mcp tools- List available toolsrad mcp prompts- List slash commandsrad mcp test- Test connectionsrad mcp auth status- Check OAuth tokens
Chat Integrationβ
apps/cli/src/commands/chat.rs:
- Loads MCP prompts into SlashCommandRegistry
- Executes slash commands via MCP
- Displays MCP command help
OAuth Flowβ
1. Server config includes OAuth auth block
β
2. OAuthTokenManager loads tokens from ~/.radium/mcp_tokens/
β
3. Before connection: Check token expiration
β
4. If expired: Refresh token using refresh_token
β
5. Bearer token injected into transport headers (SSE/HTTP)
β
6. Token saved for future use
Content Handlingβ
MCP tools can return rich content:
- Text: Direct string content
- Image: Base64 or URL, saved to temp file
- Audio: Base64 or URL, saved to temp file
ContentHandler parses and serializes content types for API compatibility.
Extension Supportβ
Extensions can provide MCP configs in JSON format:
- Loaded from extension directories
- Lower precedence than workspace configs
- Supports all transport types
Future Enhancementsβ
- Performance optimization (connection pooling)
- Advanced OAuth flows (PKCE)
- Binary content types
- Tool schema validation
- Prompt templates