Build a Python research agent with MCP
Build an agent that reads a Hacker News profile and reports its current karma and bio. Choose CrewAI or LangChain below. Both agents use a Composio session that exposes one public, read-only tool over MCP, so you don't need to connect a Hacker News account.
The session supplies the MCP URL and authentication headers. Your framework handles tool discovery and execution through that endpoint.
Set up your environment
Use Python 3.12 and create a virtual environment:
mkdir python-mcp-agent
cd python-mcp-agent
python3.12 -m venv .venv
source .venv/bin/activateSet your Composio project API key and OpenAI API key in this shell:
export COMPOSIO_API_KEY="your-composio-api-key"
export OPENAI_API_KEY="your-openai-api-key"
export COMPOSIO_EXAMPLES_USER_ID="mcp-research-demo"COMPOSIO_EXAMPLES_USER_ID identifies this demo's Composio user. In your application, use your signed-in user's ID.
Run the agent
Choose one framework and install its dependencies. Each script creates the session, runs the lookup, and deletes the session when the run finishes or raises an error.
Install Composio, CrewAI, and its MCP dependency:
python -m pip install "composio>=0.17.1" crewai mcpSave this as crewai_agent.py. CrewAI's MCPServerHTTP accepts the session's URL and headers through the agent's mcps list.
import os
from composio import Composio, SESSION_PRESET_DIRECT_TOOLS
from crewai import Agent, Crew, Task
from crewai.mcp import MCPServerHTTP
composio = Composio(api_key=os.environ["COMPOSIO_API_KEY"])
session = composio.create(
user_id=os.environ["COMPOSIO_EXAMPLES_USER_ID"],
toolkits=["hackernews"],
tools={"hackernews": {"enable": ["HACKERNEWS_GET_USER"]}},
session_preset=SESSION_PRESET_DIRECT_TOOLS,
mcp=True,
)
try:
agent = Agent(
role="Hacker News researcher",
goal="Report public profile information from live tool results.",
backstory="You check the source before writing a short profile.",
llm="gpt-5.2",
mcps=[
MCPServerHTTP(
url=session.mcp.url,
headers=session.mcp.headers,
streamable=True,
)
],
)
task = Task(
description=(
"Look up the Hacker News user pg with the available tool. "
"Report their current karma and summarize their bio if present. "
"Include https://news.ycombinator.com/user?id=pg as the source. "
"If the lookup fails, report the error instead of guessing."
),
expected_output="A short profile with username, karma, bio, and source URL.",
agent=agent,
)
result = Crew(agents=[agent], tasks=[task]).kickoff()
print(result.raw)
finally:
session.delete()Run it:
python crewai_agent.pyThe repository's CrewAI MCP example uses the same connection pattern to summarize Gmail. That variant needs a connected Gmail account.
Check the result
The agent prints pg's current karma, a short bio when the profile contains one, and the Hacker News profile URL. Values and wording vary between runs. Check that the response cites https://news.ycombinator.com/user?id=pg and uses the tool's result.
The direct tools preset exposes HACKERNEWS_GET_USER directly. The agent doesn't need to search for tools or start an authentication flow. To research another Hacker News user, change pg in the prompt and source URL.
If the MCP connection returns an authentication error, check COMPOSIO_API_KEY and pass both session.mcp.url and session.mcp.headers. Re-run the script to create a fresh session; the previous run's session was deleted.
Use the same pattern in your application
Keep the session for as long as the conversation needs it, then delete it. To use a toolkit that needs an account, authorize that toolkit before running the agent.
For SDK modifiers or custom tools that run in your process, use the CrewAI provider or LangChain provider. Those features don't run through a hosted MCP session.