Skip to content

Documentation navigation

MCP Integration

Registering MCP servers over stdio and HTTP, including auto-setup for third-party servers.

#MCP Integration

These examples wire external tool servers into an agent. MCP (Model Context Protocol) lets agents call tools that live in another process: local stdio servers, remote HTTP servers, or third-party servers installed on demand.

Register an MCP server to make its tools available to the agent: the agent picks when to call them, the SDK handles the MCP wire protocol from your application process.

#Stdio server (local)

Run a local Python MCP server over stdio:

Python
import sys
from pathlib import Path
from maivn import Agent, MCPServer

stdio_mcp = MCPServer(
    name='local_stdio',
    transport='stdio',
    command=sys.executable,
    args=[str(Path('path/to/stdio_server.py'))],
)

agent = Agent(name='Stdio MCP Agent', system_prompt='...', api_key='...')
agent.register_mcp_servers([stdio_mcp])

Registering a server does not start it. When the agent first compiles its tools, the SDK spawns the server, runs MCP initialize/tools/list, and exposes each remote tool to the agent as local_stdio__<tool_name> (the server name is prepended to disambiguate when multiple servers are attached).

#HTTP server (remote)

The HTTP transport is identical from the SDK's perspective. Just point the server at a URL:

Python
http_mcp = MCPServer(
    name='local_http',
    transport='http',
    url='http://127.0.0.1:8080/mcp',
)

agent.register_mcp_servers([http_mcp])

The agent calls local_http__<tool_name> exactly like a stdio server's tools. Configure headers and authentication on MCPServer; its max_calls_per_minute and max_calls_per_day fields limit tool calls (see Rate limits and quotas).

#Multiple servers on one agent

Mix transports freely:

Python
from maivn.messages import HumanMessage

agent.register_mcp_servers([
    MCPServer(name='local_http', transport='http', url='http://127.0.0.1:8080/mcp'),
    MCPServer(name='local_stdio', transport='stdio', command=sys.executable, args=['server.py']),
])

agent.invoke([HumanMessage(content=(
    "Call local_http__echo_http with text='hello', then local_stdio__add_numbers "
    "with a=3 and b=5. Return the MCPProtocolSummary tool."
))])

#Third-party stdio servers via uvx

For installable third-party servers (e.g. mcp-server-fetch), the auto-setup helper drops them into an isolated environment without polluting your global Python:

Python
from maivn import Agent, MCPAutoSetup, MCPServer

fetch_server = MCPServer(
    name='fetch',
    transport='stdio',
    auto_setup=MCPAutoSetup(
        provider='uvx',
        package='mcp-server-fetch',
    ),
    stdio_response_timeout_seconds=30,
)

agent = Agent(name='Fetch Agent', system_prompt='...', api_key='...')
agent.register_mcp_servers([fetch_server])

MCPAutoSetup launches the server with uvx mcp-server-fetch, which installs the package into an isolated cache on first use, so uvx must be on PATH. The transport does not retry calls. Handle provider throttling in the server or application.

#Rate limits and quotas

max_calls_per_minute and max_calls_per_day limit how often this process calls one MCPServer:

Python
from maivn import MCPServer

limited = MCPServer(
    name='quota_limited',
    transport='http',
    url='http://127.0.0.1:8080/mcp',
    max_calls_per_minute=30,
    max_calls_per_day=2000,
)

Over the minute limit, a call waits for a free slot. Over the daily limit, it raises MCPRuntimeError without contacting the server. Both values must be at least 1. The counts live in your process, so they do not replace the upstream server's own quota. Apply bounded retries in your server or application.

#Combining MCP with structured output

MCP tools work the same way as registered tools, including with final_tool and structured_output:

Python
from pydantic import BaseModel, Field
from maivn.messages import HumanMessage

messages = [HumanMessage(content='Call both MCP tools, then return the summary.')]

class MCPProtocolSummary(BaseModel):
    """Capture MCP protocol results."""
    http_result: dict = Field(..., description='Result from HTTP MCP tool')
    stdio_result: dict = Field(..., description='Result from stdio MCP tool')
    notes: str = Field(default='Protocol demo complete.')

agent.toolify(name='mcp_protocol_summary', final_tool=True)(MCPProtocolSummary)

agent.structured_output(model=MCPProtocolSummary).invoke(messages)

#Closing connections

When the agent is done, close the MCP connections so subprocesses and sockets shut down cleanly. Agent.close() stops every MCP server registered on the agent. It is safe to call twice, and a stopped server starts again on its next use:

Python
try:
    agent.invoke(messages)
finally:
    agent.close()

In long-running services, attach this to the process lifecycle (e.g. FastAPI's shutdown hook).

#What's next

  • Connecting MCP Servers — the concept guide: where MCP tools run, how to choose a transport, how to authenticate and what failures look like.
  • Agents & Tools — combining MCP tools with registered tools and hooks, plus cross-agent dependencies.
  • Real-World Projects — the financial-planner project uses MCP servers for live market data.
  • MCP API reference: docs/api/mcp.md.