MCPServer
autogen.beta.mcp.server.MCPServer #
MCPServer(agent, *, name=None, version=None, instructions=None, website_url=None, icons=None, tool_name='ask', tool_description=None, stream_progress=True, context_provider=None, lifespan=None, sessions=True, resources=(), resource_templates=(), prompts=(), path='/mcp', stateless=False, json_response=False, security=None)
Wrap an AG2 :class:Agent as an MCP server.
The agent is exposed as a single conversational tool (ask by default) that runs :meth:Agent.ask and returns the reply — the inverse of the consume-side toolkit autogen.beta.tools.MCPToolkit, which connects to an MCP server.
The instance is itself an ASGI3 application: it serves MCP over streamable HTTP and manages its own lifespan, so a standalone uvicorn run just works::
app = MCPServer(agent, path="/mcp")
uvicorn.run(app, host="127.0.0.1", port=8000)
For local clients (Claude Desktop, Cursor, the MCP Inspector), :meth:run_stdio serves over stdin/stdout instead. The HTTP transport parameters (path, stateless, json_response, security) are ignored over stdio.
name / version / instructions / website_url / icons populate the initialize handshake's serverInfo + instructions. instructions is client-facing "how to use this server" guidance — it is not derived from the agent's system prompt (which is internal); pass it explicitly when you want to advertise usage hints.
sessions controls multi-turn history. By default (True) each MCP session (keyed by the transport's mcp-session-id, or a per-process key over stdio) keeps its own conversation history that accumulates across calls; pass a :class:~autogen.beta.mcp.sessions.SessionConfig to tune the bound / TTL / backend, or False to make every call stateless. A stateless HTTP transport (stateless=True) issues no session id, so it stays stateless regardless of this setting.
resources / resource_templates / prompts expose MCP resources and prompts alongside the conversational tool; the corresponding capability is advertised only when a non-empty collection is supplied.
Source code in autogen/beta/mcp/server.py
run_stdio async #
Serve the agent over stdio until the client disconnects.