How to Connect an AI Agent to an MCP Server (Python Client)
Updated 2026-11-25
Claude Desktop and Cursor are MCP clients, but your own agent can be one too. This guide connects a Python agent to any stdio MCP server, converts the server’s tools into LLM tool definitions, and routes tool calls back to the server. If you have not built a server yet, start with Build an MCP server in Python or a hosted recipe such as the GitHub, Notion, Jira, or Postgres MCP guides. When the client shows connection closed, work through Fix: MCP server connection closed (and Fix: MCP server not connecting when tools never load).
Prerequisites
- Python 3.10+,
pip install mcp openai, and the tool loop from Build your first AI agent in Python - An MCP server to connect to. We use the one from Build an MCP server in Python, but any stdio server works (for example
npx -y @modelcontextprotocol/server-filesystem /tmp).
Steps
1. Open a client session
# mcp_agent.py
import asyncio, json, os
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
from openai import OpenAI
SERVER = StdioServerParameters(command="python", args=["server.py"])
llm = OpenAI()
MODEL = os.environ.get("MODEL", "gpt-5-mini")
2. Convert MCP tools to OpenAI tool definitions
MCP tools already carry a JSON Schema in inputSchema, so the mapping is direct:
def to_openai_tools(mcp_tools):
return [{"type": "function", "function": {
"name": t.name,
"description": t.description or "",
"parameters": t.inputSchema}} for t in mcp_tools]
3. Run the agent loop, calling tools through the session
async def main(goal: str):
async with stdio_client(SERVER) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
tools = (await session.list_tools()).tools
print("Tools:", [t.name for t in tools])
messages = [{"role": "user", "content": goal}]
for _ in range(8): # cap steps; see Fix: AI agent stuck in a loop
msg = llm.chat.completions.create(model=MODEL, messages=messages,
tools=to_openai_tools(tools)).choices[0].message
if not msg.tool_calls:
print(msg.content); return
messages.append(msg)
for call in msg.tool_calls:
result = await session.call_tool(call.function.name,
json.loads(call.function.arguments or "{}"))
text = "\n".join(c.text for c in result.content if c.type == "text")
messages.append({"role": "tool", "tool_call_id": call.id, "content": text})
asyncio.run(main("What's the weather in Lisbon right now?"))
4. Run it
python mcp_agent.py
The client launches the server as a subprocess, lists its tools, and the model calls current_weather with Lisbon’s coordinates.
5. Connect to a remote (HTTP) server instead
from mcp.client.streamable_http import streamablehttp_client
async with streamablehttp_client("https://example.com/mcp") as (read, write, _):
async with ClientSession(read, write) as session:
...
Using a framework instead
- OpenAI Agents SDK:
from agents.mcp import MCPServerStdioand passmcp_servers=[...]toAgent(see the MCP section in that guide). - LangChain/LangGraph: the
langchain-mcp-adapterspackage loads MCP tools as LangChain tools. If the graph hits a recursion limit, see Fix: LangGraph recursion limit.
Common errors
McpError: Connection closed right away: the server crashed on start or the session closed early. Run its command by hand to see the traceback.
result.isError is true: the tool raised an exception. The error text is in result.content. Pass it back to the model so it can correct its arguments.
Hangs on initialize(): the server writes non-protocol text to stdout. Servers must log to stderr only.
FAQ
Can my own Python agent talk to an MCP server?
Yes. This guide uses the official MCP Python SDK. It lists the server’s tools, converts them for the LLM, and sends each tool call back to the server.
Which server can I connect?
Any stdio MCP server. The example is the server from Build an MCP server in Python.
Is Claude Desktop required?
No. Claude Desktop and Cursor are MCP clients. This guide makes your own agent the client.
Related guides
- Build an MCP server in Python (
MCPServerserver this client calls) - Notion MCP server
- GitHub MCP server (hosted tools in Cursor)
- MCP server for Postgres and Connect an AI agent to a database (SQL without MCP)
- MCP server for Jira
- OpenAI Agents SDK function tools (framework MCP wrapper)
- Fix: MCP server connection closed
- Fix: MCP server not connecting
- Claude Agent SDK: add an MCP server
- Fix: AI agent stuck in a loop and Fix: LangGraph recursion limit
- What is an AI agent? (MCP in the agent stack)