How to Connect MCP Servers to AI Agents for External Tool Integration
Connecting MCP servers to AI agents requires wrapping external API connections with the Model Context Protocol (MCP) via the MCPTools class, allowing LLMs to discover and invoke remote tools through standardized JSON-RPC interfaces.
The Arindam200/awesome-ai-apps repository demonstrates production-ready patterns for connecting MCP servers to AI agents for external tool integration. By implementing the Model Context Protocol, developers can expose any HTTP-based service as a callable tool that LLM agents invoke like native functions, eliminating hard-coded API integrations.
Understanding the MCP Server-Client Architecture
The Model Context Protocol creates a bidirectional bridge between external services and AI agents. At the core of this pattern are two components: the MCP Server exposing tool definitions via JSON-RPC, and the MCP Client managing the session and request marshaling.
MCP Server Implementation
MCP servers wrap external capabilities as discoverable tools. According to the source code in mcp_ai_agents/custom_mcp_server/mcp-server.py, servers register functions using decorators that expose them as RPC endpoints:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("email")
@mcp.tool()
def send_email(receiver_email: str, subject: str, body: str) -> dict:
"""Send an email via Gmail SMTP."""
return {"success": True, "message": "Email sent"}
This FastMCP server exposes send_email and configure_email as tools callable by any MCP-enabled agent.
MCP Client Integration
On the client side, agents instantiate MCPTools bound to a ClientSession. The file mcp_ai_agents/taskade_mcp_agent/main.py demonstrates this pattern by creating StdioServerParameters and opening a session with MCPTools(session=session). The client handles request/response marshaling automatically.
Connecting STDIO-Based MCP Servers
STDIO transport runs MCP servers as local subprocesses, ideal for Node.js-based servers distributed via npm.
GitHub MCP Server Example
The starter implementation in mcp_ai_agents/mcp_starter/main.py connects to the GitHub MCP server using MCPServerStdio:
async with MCPServerStdio(
cache_tools_list=True,
params={
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {"GITHUB_PERSONAL_ACCESS_TOKEN": os.getenv("GITHUB_PERSONAL_ACCESS_TOKEN")},
},
) as server:
agent = Agent(
name="GitHub Assistant",
instructions="Use the list_issues and list_commits MCP tools to analyse the repo.",
mcp_servers=[server],
model=OpenAIChatCompletionsModel(model="meta-llama/Meta-Llama-3.1-8B-Instruct"),
)
result = await Runner.run(starting_agent=agent, input="Show the latest issue in arindam200/awesome-ai-apps")
The mcp_servers=[mcp_server] parameter injects the GitHub tools directly into the agent's context.
Taskade Integration with Streamlit
The mcp_ai_agents/taskade_mcp_agent/main.py file extends this pattern with a full Streamlit UI, building StdioServerParameters to connect to @taskade/mcp-server and passing the session to MCPTools.
Connecting HTTP-Based MCP Gateways
For remote services, the streamable-http transport eliminates local subprocess management. The Docs Q&A agent in mcp_ai_agents/docs_qna_agent/main.py demonstrates connecting to a remote documentation gateway:
mcp_tools = MCPTools(url="https://docs.tokenfactory.nebius.com/mcp", transport="streamable-http")
await mcp_tools.connect()
agent = Agent(
tools=[mcp_tools],
instructions="Answer user questions using the documentation MCP endpoint.",
model=Nebius(id="deepseek-ai/DeepSeek-V3-0324", api_key=os.getenv("NEBIUS_API_KEY")),
)
response = await agent.arun("How do I add a new MCP server?")
This pattern suits microservices architectures where MCP servers run as persistent cloud endpoints.
Integrating MCP Tools with Agent Frameworks
Agent frameworks like Agno and the Agents SDK consume MCP tools through standardized interfaces. The Hotel Finder agent in mcp_ai_agents/hotel_finder_agent/main.py illustrates advanced integration with dynamic response templates.
Dynamic Instruction Prompts
The agent's system prompt explicitly instructs the LLM to use MCP tools rather than hallucinating data. Lines 38-44 define instructions requiring the model to call airbnb_search via MCP and format output according to dynamic templates reflecting search modes (quick vs. advanced).
Tool Discovery and Execution
When instantiating agents, pass the MCPTools instance via the tools parameter:
agent = Agent(
tools=[mcp_tools],
instructions="Use available MCP tools for any external data fetch.",
model=ModelConfig()
)
The LLM automatically generates JSON-RPC calls when it detects a need for external data, with the MCPTools wrapper handling serialization and transport.
Building Custom MCP Servers
For proprietary business logic, FastMCP enables rapid server development. The repository's mcp_ai_agents/custom_mcp_server/mcp-server.py implements an email service exposing configure_email and send_email as MCP tools. Any conforming server becomes instantly accessible to agents by changing the connection parameters.
Summary
Connecting MCP servers to AI agents for external tool integration follows a consistent pattern across the Arindam200/awesome-ai-apps repository:
- MCP Servers expose external APIs via JSON-RPC using
FastMCPor official SDKs - Transport layers support both local STDIO (for npm-based servers) and remote HTTP (for cloud gateways)
- Client wrappers like
MCPToolsandMCPServerStdiomanage sessions and tool discovery - Agent frameworks inject tools through
mcp_serversortoolsparameters, with prompts ensuring model compliance - Dynamic templates in agents like the Hotel Finder ensure consistent output formatting without post-processing
Frequently Asked Questions
What transport protocols does MCP support for connecting servers to agents?
MCP supports multiple transport mechanisms. The awesome-ai-apps repository demonstrates STDIO for local subprocess communication (used with npx packages like @taskade/mcp-server) and streamable-http for remote cloud gateways (used in the Docs Q&A agent connecting to https://docs.tokenfactory.nebius.com/mcp). The MCPTools class accepts a transport parameter to specify the protocol.
How does an AI agent discover available tools from an MCP server?
Tool discovery happens automatically when the MCPTools wrapper initializes. In mcp_ai_agents/mcp_starter/main.py, setting cache_tools_list=True during MCPServerStdio creation prompts the client to fetch and cache available tool definitions from the server. The agent framework then incorporates these tools into the LLM's context, allowing the model to recognize when to invoke external functions.
Can I build custom MCP servers for internal company APIs?
Yes. The mcp_ai_agents/custom_mcp_server/mcp-server.py file demonstrates building proprietary MCP servers using FastMCP. You register functions with the @mcp.tool() decorator to expose internal capabilities (like email sending or database queries) as standardized MCP tools. Any agent configured with the appropriate client parameters can then invoke these internal services without code changes to the agent logic.
What is the difference between using MCPServerStdio and MCPTools in the codebase?
MCPServerStdio (shown in mcp_ai_agents/mcp_starter/main.py) is a high-level context manager specifically for STDIO-based servers that handles process lifecycle management. MCPTools (used in mcp_ai_agents/docs_qna_agent/main.py) is the underlying wrapper that manages the JSON-RPC session and tool invocation interface. STDIO servers typically use both, while HTTP gateways use MCPTools directly with a URL parameter.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →