🔧 You are viewing: API Specification - Complete technical referenceSee also: MCP Client · API Specification
Source Code:
src/gaia/mcp/mixin.py- Agent mixinsrc/gaia/mcp/client/mcp_client.py- Core clientsrc/gaia/mcp/client/mcp_client_manager.py- Connection managersrc/gaia/mcp/client/config.py- Configurationsrc/gaia/mcp/client/transports/- Transport implementations
Import:
from gaia.mcp import MCPClientMixin, MCPClient, MCPClientManagerOverview
The MCP Client enables GAIA agents to connect to and use tools from external MCP (Model Context Protocol) servers. This provides universal tool integration - any GAIA agent can use any MCP server’s tools with zero code changes. Key Features:- Universal Tool Access: Connect to any stdio MCP server and use its tools
- Automatic Tool Registration: MCP tools automatically appear in agent’s tool registry
- Tool Namespacing: Multiple servers with same tool names work without conflicts
- Stdio Transport: Support for subprocess-based MCP servers
- Configuration Management: Persistent server configurations
- CLI Integration: Simple command-line interface for managing connections
- GAIA Response Wrapping: Tool responses formatted with status/message/data fields
Requirements
Functional Requirements
-
Server Connection
- Connect to MCP servers via stdio (subprocess)
- Initialize MCP protocol handshake (JSON-RPC 2.0)
- Handle connection failures gracefully
- Support configurable timeout (default: 30 seconds)
-
Tool Discovery
- List all available tools from connected servers (
tools/list) - Parse MCP tool schemas (JSON Schema format)
- Convert MCP schemas to GAIA
_TOOL_REGISTRYformat - Cache tool schemas (refresh on demand)
- List all available tools from connected servers (
-
Tool Execution
- Call MCP tools with proper arguments (
tools/call) - Handle tool responses and errors
- Format validation errors with schema context
- Wrap responses in GAIA-style format (status/message/data/instruction)
- Call MCP tools with proper arguments (
-
Tool Registration
- Register MCP tools in agent’s
_TOOL_REGISTRY - Namespace tools by server name (prevent collisions)
- Add server context to tool descriptions (
[MCP:servername]) - Unregister tools when server disconnects
- Register MCP tools in agent’s
-
Configuration Management
- Save server configurations to
~/.gaia/mcp_servers.json - Load servers on startup
- Support adding/removing servers via CLI and API
- Save server configurations to
Non-Functional Requirements
-
Performance
- Minimal overhead for tool calls (
<50ms) - Efficient subprocess management
- Tool schema caching (avoid repeated
tools/listcalls) - Lazy loading of tools
- Minimal overhead for tool calls (
-
Reliability
- Process cleanup on disconnect (terminate, then kill after 5s)
- Error recovery and logging
- Graceful handling of dead processes
- Connection state tracking
-
Usability
- Simple mixin interface for agents
- Clear CLI commands (
gaia mcp add/list/tools/remove) - Helpful error messages with schema context
- Debug mode for troubleshooting
API Specification
MCPClientMixin
The mixin class that adds MCP client capabilities to GAIA agents.Constructor
connect_mcp_server
load_mcp_servers_from_config
disconnect_mcp_server
list_mcp_servers
get_mcp_client
_register_mcp_tools (Internal)
_unregister_mcp_tools (Internal)
Quick Reference
MCPClient
The client class for interacting with an MCP server.Constructor
from_config (Class Method)
from_command (Class Method - Legacy)
connect
disconnect
is_connected
list_tools
call_tool
create_tool_wrapper
Quick Reference
MCPClientManager
The manager class for handling multiple MCP client connections.Constructor
add_server
remove_server
get_client
list_servers
disconnect_all
load_from_config
Quick Reference
MCPConfig
Configuration manager for MCP servers.Constructor
add_server
remove_server
get_server
get_servers
server_exists
Quick Reference
MCPTransport (Abstract Base Class)
Abstract base class for MCP transport implementations.connect (Abstract)
disconnect (Abstract)
send_request (Abstract)
is_connected (Abstract)
StdioTransport
Stdio-based transport using subprocess for MCP servers.Constructor
connect
disconnect
send_request
is_connected
MCPTool
Data class representing an MCP tool with its schema.mcp_tool_requires_confirmation
TOOLS_REQUIRING_CONFIRMATION set. The classification therefore
travels with the registry entry as requires_confirmation, which
Agent._tool_requires_confirmation reads at execution time.
to_gaia_format
Implementation Details
Connection Flow
Tool Registration Pattern
MCP tools are converted from JSON Schema format to GAIA’s tool registry format:Tool Namespacing
MCP tools are automatically namespaced to prevent collisions:- MCP Tool:
read_file - GAIA Tool:
mcp_filesystem_read_file - Description:
[MCP:filesystem] Read file contents from disk
GAIA Response Wrapping
Tool responses are wrapped in GAIA-style format to help the LLM interpret results:JSON-RPC Protocol
The MCP client uses JSON-RPC 2.0 for communication: Request format:Error Handling
Validation errors are enhanced with schema context:Configuration
Configuration File
Servers are saved to~/.gaia/mcp_servers.json using the Anthropic-compatible format:
Environment Variables
MCP servers may require environment variables. Pass them via theenv field:
env field passes variables directly to the subprocess without shell expansion, making it secure and portable.
CLI Commands
;, |, &&, >) — and a backtick anywhere in the string — is rejected rather than interpreted. Use the config format’s args list for values that must contain those characters.
See CLI Reference for complete documentation.
Testing Requirements
Unit Tests
The MCP client has comprehensive unit tests intests/unit/mcp/client/:
Running Tests
Test Patterns
Tests use mocking to avoid requiring real MCP servers:Usage Examples
Agent with Single MCP Server
Agent with Multiple MCP Servers
Loading Servers from Configuration
Direct Client Usage (Without Agent)
Using MCPClientManager
Error Handling
Debug Mode
Enable detailed logging:Related Documentation
- MCP Client - SDK reference for connecting to MCP servers
- Agent System - Agent framework
- Tool Decorator - GAIA tool system
- CLI Reference - CLI commands