Skip to content

Python: Add MCP hosting adapter package#351

Open
hashwnath wants to merge 1 commit intomicrosoft:mainfrom
hashwnath:feature/mcp-hosting-adapter-321
Open

Python: Add MCP hosting adapter package#351
hashwnath wants to merge 1 commit intomicrosoft:mainfrom
hashwnath:feature/mcp-hosting-adapter-321

Conversation

@hashwnath
Copy link
Copy Markdown

Summary

Addresses #321

Introduces a new microsoft-agents-hosting-mcp package that exposes an M365 Agent as an MCP (Model Context Protocol) server, enabling MCP clients to discover and invoke agent capabilities through the standard MCP protocol.

Architecture

The adapter follows the established hosting adapter pattern used by hosting-aiohttp and hosting-fastapi:

  • Subclasses ChannelAdapter and implements send_activities to buffer responses
  • Translates MCP tool calls into Activity-based turns routed through the standard middleware → agent pipeline
  • Uses FastMCP from the mcp Python SDK for protocol handling and transport
  • Supports streamable HTTP transport for production deployment

Key Components

  • MCPAdapter — Core adapter bridging MCP protocol to the Agents SDK pipeline
    • Built-in message tool for text-based agent interaction
    • Custom tool registration for exposing multiple agent capabilities
    • Full middleware pipeline support
    • Response buffering via TurnContext.turn_state
  • MCP_CHANNEL_ID — Channel identifier ("mcp") for MCP-originated activities

Usage

from microsoft_agents.hosting.mcp import MCPAdapter

adapter = MCPAdapter(agent=my_agent, server_name="my-agent-mcp")

# Mount on FastAPI
app.mount("/mcp", adapter.streamable_http_app())

Test plan

  • 14 unit tests covering:
    • Adapter initialization (default/custom server names, ASGI app creation)
    • Turn processing (echo, multi-response, silent agent, tool name propagation, channel ID)
    • send_activities response buffering
    • Unsupported operations (update_activity, delete_activity)
    • Middleware pipeline integration (invocation and activity modification)
    • Custom tool registration
  • black formatted (88 char line length)
  • flake8 clean (127 max line length)
  • All tests pass on Python 3.11

🤖 Generated with Claude Code

Introduce a new hosting adapter that exposes an M365 Agent as an MCP
server using the mcp Python SDK (FastMCP). This enables MCP clients to
discover and invoke agent capabilities through the standard MCP protocol.

The adapter follows the existing hosting adapter pattern:
- Subclasses ChannelAdapter and implements send_activities
- Translates MCP tool calls into Activity-based turns
- Routes turns through the standard middleware → agent pipeline
- Buffers agent responses and returns them as MCP tool results

Package includes:
- MCPAdapter with built-in "message" tool and custom tool support
- Streamable HTTP transport via FastMCP
- Middleware pipeline integration
- 14 unit tests covering init, turn processing, middleware, and edge cases

Addresses microsoft#321

Fixes microsoft#321
@hashwnath hashwnath requested a review from a team as a code owner April 2, 2026 05:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant