MCP

The MCP plugin enables Daita agents to connect to any Model Context Protocol server and autonomously use their tools via LLM function calling. MCP is Anthropic's open standard for connecting AI systems to external data sources and tools.

#Overview

The MCP integration allows agents to discover and use tools from external MCP servers without manual configuration. When you attach an MCP server to an agent, the agent automatically:

  1. Connects to the MCP server via stdio transport
  2. Discovers all available tools from the server
  3. Converts MCP tools to the agent's unified tool format
  4. Routes tool calls to the appropriate MCP server
  5. Manages connection lifecycle and error handling


Key Features:

  • Zero-configuration tool discovery from MCP servers
  • Multiple simultaneous MCP server connections
  • Automatic tool registration in agent tool registry
  • Thread-safe concurrent tool execution
  • Built-in connection pooling and lifecycle management
  • Compatible with all official MCP servers

#Quick Start

python
from daita import Agent
from daita.plugins import mcp
 
# Agent with filesystem MCP server
agent = Agent(
    name="file_analyzer",
    mcp=mcp.server(
        command="uvx",
        args=["mcp-server-filesystem", "/data"]
    )
)
 
await agent.start()
 
# Agent autonomously discovers and uses filesystem tools
result = await agent.run("Read report.csv and calculate totals")

#MCP Server Configuration

#Single Server

Connect to a single MCP server using the mcp.server() factory function:

python
from daita import Agent
from daita.plugins import mcp
 
# Filesystem server
agent = Agent(
    name="file_agent",
    mcp=mcp.server(
        command="uvx",
        args=["mcp-server-filesystem", "/data"]
    )
)
 
# GitHub server with authentication
agent = Agent(
    name="github_agent",
    mcp=mcp.server(
        command="npx",
        args=["-y", "@modelcontextprotocol/server-github"],
        env={"GITHUB_TOKEN": "ghp_your_token"}
    )
)

#Multiple Servers

Connect to multiple MCP servers simultaneously:

python
from daita import Agent
from daita.plugins import mcp
import os
 
agent = Agent(
    name="multi_tool_agent",
    mcp=[
        # Filesystem access
        mcp.server(
            command="uvx",
            args=["mcp-server-filesystem", "/data"],
            name="filesystem"
        ),
        # GitHub integration
        mcp.server(
            command="npx",
            args=["-y", "@modelcontextprotocol/server-github"],
            env={"GITHUB_TOKEN": os.getenv("GITHUB_TOKEN")},
            name="github"
        ),
        # Database access
        mcp.server(
            command="python",
            args=["-m", "mcp_server_postgres"],
            env={"DATABASE_URL": os.getenv("DATABASE_URL")},
            name="postgres"
        )
    ]
)
 
# Agent has access to tools from all three servers

#Server Configuration Function

The mcp.server() function creates an MCP server configuration:

python
mcp.server(
    command: str,
    args: Optional[List[str]] = None,
    env: Optional[Dict[str, str]] = None,
    name: Optional[str] = None
) -> Dict[str, Any]

#Parameters

ParameterTypeRequiredDescription
commandstrYesCommand to run MCP server (e.g., "uvx", "npx", "python")
argsList[str]NoArguments for the command
envDict[str, str]NoEnvironment variables for the server process
namestrNoOptional name for the server (for logging/debugging)

#Returns

Server configuration dictionary used internally by the agent.

#Agent Integration

When you attach MCP servers to an agent, tools are automatically discovered and registered:

python
from daita import Agent
from daita.plugins import mcp
 
agent = Agent(
    name="filesystem_agent",
    mcp=mcp.server(command="uvx", args=["mcp-server-filesystem", "/data"])
)
 
await agent.start()
 
# Tools are discovered automatically on first run() call
result = await agent.run("Read the file data/report.csv")
 
# Agent autonomously uses MCP tools as needed

#Official MCP Servers

Daita works with all official MCP servers. Here are common examples:

#Filesystem Server

Access local files and directories:

python
mcp.server(
    command="uvx",
    args=["mcp-server-filesystem", "/data"]
)

Available Tools:

  • read_file - Read file contents
  • write_file - Write to files
  • list_directory - List directory contents
  • search_files - Search for files

#GitHub Server

Interact with GitHub repositories:

python
mcp.server(
    command="npx",
    args=["-y", "@modelcontextprotocol/server-github"],
    env={"GITHUB_TOKEN": os.getenv("GITHUB_TOKEN")}
)

Available Tools:

  • create_issue - Create GitHub issues
  • get_repository - Get repository information
  • list_issues - List repository issues
  • create_pull_request - Create PRs

#PostgreSQL Server

Query PostgreSQL databases:

python
mcp.server(
    command="python",
    args=["-m", "mcp_server_postgres"],
    env={"DATABASE_URL": "postgresql://user:pass@localhost/db"}
)

Available Tools:

  • execute_query - Run SQL queries
  • list_tables - List database tables
  • describe_table - Get table schema

#Slack Server

Send messages and interact with Slack:

python
mcp.server(
    command="npx",
    args=["-y", "@modelcontextprotocol/server-slack"],
    env={"SLACK_TOKEN": os.getenv("SLACK_TOKEN")}
)

Available Tools:

  • post_message - Send messages to channels
  • list_channels - List workspace channels
  • get_channel_history - Get message history

#Custom MCP Servers

#Creating a Custom Server

You can create custom MCP servers in Python:

python
# custom_mcp_server.py
from mcp.server import Server
from mcp.types import Tool
 
server = Server("custom-tools")
 
@server.list_tools()
async def list_tools():
    return [
        Tool(
            name="calculate_sum",
            description="Add two numbers",
            inputSchema={
                "type": "object",
                "properties": {
                    "a": {"type": "number"},
                    "b": {"type": "number"}
                },
                "required": ["a", "b"]
            }
        )
    ]
 
@server.call_tool()
async def call_tool(name: str, arguments: dict):
    if name == "calculate_sum":
        return {"result": arguments["a"] + arguments["b"]}
    raise ValueError(f"Unknown tool: {name}")
 
if __name__ == "__main__":
    server.run()

#Using Custom Server

python
from daita import Agent
from daita.plugins import mcp
 
agent = Agent(
    name="custom_agent",
    mcp=mcp.server(
        command="python",
        args=["custom_mcp_server.py"]
    )
)
 
await agent.start()
 
# Use custom tools
result = await agent.run("Calculate the sum of 42 and 58")

#Error Handling

MCP connections handle errors gracefully. Wrap agent operations in try-except:

python
from daita import Agent
from daita.plugins import mcp
 
try:
    agent = Agent(
        name="agent",
        mcp=mcp.server(command="uvx", args=["mcp-server-filesystem", "/data"])
    )
    await agent.start()
    result = await agent.run("Read /data/file.txt")
 
except ConnectionError as e:
    print(f"MCP server connection failed: {e}")
except RuntimeError as e:
    print(f"Tool execution failed: {e}")
finally:
    await agent.stop()

#Best Practices

Server Configuration:

  • Use environment variables for API keys and tokens, never hardcode
  • Name your servers with the name parameter for easier debugging
  • Test MCP server commands manually before using with agents
  • Handle connection failures with try-except blocks

Performance:

  • Reuse agents instead of creating new ones for each request
  • Connections are maintained automatically - no manual pooling needed
  • Always call agent.stop() or use async context managers for cleanup
  • MCP tools are discovered lazily on first run() call

Debugging:

  • Enable debug logging: logging.basicConfig(level=logging.DEBUG)
  • Use display_reasoning=True to see agent decisions
  • Check agent.tool_names to verify tools were discovered
  • Test server commands manually: uvx mcp-server-filesystem /data

Tool Name Conflicts:

  • If multiple servers provide the same tool name, last server wins
  • A warning is logged when tool name collisions occur
  • Use unique tool names in custom MCP servers

#Integration with Plugins

MCP tools work alongside Daita's native plugins:

python
from daita import Agent
from daita.plugins import mcp, PostgreSQLPlugin
from daita.core.tools import tool
 
# Combine MCP tools with native plugins
db_plugin = PostgreSQLPlugin(host="localhost", database="mydb")
 
agent = Agent(
    name="hybrid_agent",
    tools=[db_plugin],    # Native plugin tools
    mcp=mcp.server(       # MCP tools
        command="uvx",
        args=["mcp-server-filesystem", "/data"]
    )
)
 
await agent.start()
 
# Agent autonomously uses both database plugin tools and MCP filesystem tools
result = await agent.run(
    "Query all users from the database and save the results to /data/users.json"
)
 
# Or create custom tools that combine both
@tool
async def hybrid_data_operation(user_count: int) -> dict:
    """Fetch users and save to file."""
    # Use native plugin
    db_results = await db_plugin.query(f"SELECT * FROM users LIMIT {user_count}")
 
    # Use MCP tool
    file_content = await agent.call_mcp_tool("write_file", {
        "path": "/data/users.json",
        "content": str(db_results)
    })
 
    return {"db_results": len(db_results), "file_written": True}
 
agent.register_tool(hybrid_data_operation)
result = await agent.run("Fetch 100 users and save them")

#Requirements

Install the MCP SDK to use MCP servers:

bash
pip install mcp

Official MCP SDK: https://github.com/modelcontextprotocol/python-sdk

#Troubleshooting

Connection Timeout:

  • Test the MCP server command manually: uvx mcp-server-filesystem /data
  • Verify the server process starts and doesn't error immediately
  • Check that the command path is correct in your environment

Tools Not Discovered:

  • MCP tools are loaded lazily on first run() call
  • Check logs with logging.basicConfig(level=logging.DEBUG)
  • Verify the server provides tools by running it manually

Import Errors:

  • Install MCP SDK: pip install mcp
  • Ensure the MCP server package is installed (e.g., uvx mcp-server-filesystem)

#Next Steps

  • Tools - Understanding the unified tool system
  • Agent - Learn about agent capabilities
  • Plugins - Native plugin integrations
  • Workflows - Building multi-agent systems

#Resources