Skip to main content
CLI update (#977): gaia mcp add / gaia mcp remove referenced in this spec were removed. MCP servers are now configured through the connectors framework (gaia connectors configure <id> --set KEY=VALUE; the entry is persisted to ~/.gaia/mcp_servers.json with secrets in the OS keyring). gaia mcp list still lists configured servers. Sections below using gaia mcp add/remove are pending a rewrite — see issue #1339.
🔧 You are viewing: API Specification - Complete technical referenceSee also: MCP Client · API Specification
Source Code:
Import: from gaia.mcp import MCPClientMixin, MCPClient, MCPClientManager

Overview

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
Architecture Overview:

Requirements

Functional Requirements

  1. 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)
  2. Tool Discovery
    • List all available tools from connected servers (tools/list)
    • Parse MCP tool schemas (JSON Schema format)
    • Convert MCP schemas to GAIA _TOOL_REGISTRY format
    • Cache tool schemas (refresh on demand)
  3. 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)
  4. 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
  5. Configuration Management
    • Save server configurations to ~/.gaia/mcp_servers.json
    • Load servers on startup
    • Support adding/removing servers via CLI and API

Non-Functional Requirements

  1. Performance
    • Minimal overhead for tool calls (<50ms)
    • Efficient subprocess management
    • Tool schema caching (avoid repeated tools/list calls)
    • Lazy loading of tools
  2. Reliability
    • Process cleanup on disconnect (terminate, then kill after 5s)
    • Error recovery and logging
    • Graceful handling of dead processes
    • Connection state tracking
  3. 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.

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
This ensures multiple servers with the same tool name work correctly:

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:
Success response:
Error response:

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:
This format is compatible with Claude Desktop and other Anthropic tools.

Environment Variables

MCP servers may require environment variables. Pass them via the env field:
Or in the config file:
The env field passes variables directly to the subprocess without shell expansion, making it secure and portable.

CLI Commands

The CLI accepts shell command strings for convenience and converts them to the config format internally. See CLI Reference for complete documentation.

Testing Requirements

Unit Tests

The MCP client has comprehensive unit tests in tests/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:


Extension Points

Custom Transport

To implement a custom transport (e.g., HTTP, WebSocket):

Custom Tool Wrapper

To customize how MCP tools are wrapped: