mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
9c57680f00
* Python: Add header_provider to MCPStreamableHTTPTool (#4808) Add a header_provider callback parameter to MCPStreamableHTTPTool that enables injecting dynamic per-request HTTP headers from runtime kwargs (originating from FunctionInvocationContext.kwargs set in agent middleware). The implementation uses contextvars and httpx event hooks to ensure headers are task-local and safe for concurrent tool calls: - header_provider receives the runtime kwargs dict and returns headers - call_tool sets a ContextVar before delegating to MCPTool.call_tool - An httpx request event hook reads from the ContextVar and injects headers Example usage: mcp_tool = MCPStreamableHTTPTool( name="web-api", url="https://api.example.com/mcp", header_provider=lambda kwargs: { "X-Auth-Token": kwargs.get("auth_token", ""), }, ) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Address review feedback for #4808: Python: [Bug]: Unable to pass AgentContext to MCPStreamableHTTPTool * Add test for header_provider via FunctionTool.invoke with FunctionInvocationContext Addresses PR review comment: exercises the full pipeline from FunctionInvocationContext.kwargs through FunctionTool.invoke to MCPStreamableHTTPTool.call_tool and header_provider, rather than testing call_tool in isolation. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Address review feedback for #4808: review comment fixes * Fix streamable MCP transport defaults Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix Azure AI test client mocks Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix MCP runtime kwarg regressions Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Stabilize MCP tool runtime kwargs Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Use context kwargs in MCP wrappers Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * updated mcp samples * fix link --------- Co-authored-by: Copilot <copilot@github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
81 lines
2.3 KiB
Python
81 lines
2.3 KiB
Python
# Copyright (c) Microsoft. All rights reserved.
|
|
|
|
from typing import Annotated, Any
|
|
|
|
import anyio
|
|
from agent_framework import Agent, tool
|
|
from agent_framework.openai import OpenAIChatClient
|
|
from dotenv import load_dotenv
|
|
|
|
# Load environment variables from .env file
|
|
load_dotenv()
|
|
|
|
"""
|
|
This sample demonstrates how to expose an Agent as an MCP server.
|
|
|
|
To run this sample, set up your MCP host (like Claude Desktop or VSCode GitHub Copilot Agents)
|
|
with the following configuration:
|
|
```json
|
|
{
|
|
"servers": {
|
|
"agent-framework": {
|
|
"command": "uv",
|
|
"args": [
|
|
"--directory=<path to project>/agent-framework/python/samples/02-agents/mcp",
|
|
"run",
|
|
"agent_as_mcp_server.py"
|
|
],
|
|
"env": {
|
|
"OPENAI_API_KEY": "<OpenAI API key>",
|
|
"OPENAI_MODEL": "<OpenAI Responses model ID>",
|
|
}
|
|
}
|
|
}
|
|
}
|
|
```
|
|
"""
|
|
|
|
|
|
# NOTE: approval_mode="never_require" is for sample brevity. Use "always_require" in production; see samples/02-agents/tools/function_tool_with_approval.py and samples/02-agents/tools/function_tool_with_approval_and_sessions.py.
|
|
@tool(approval_mode="never_require")
|
|
def get_specials() -> Annotated[str, "Returns the specials from the menu."]:
|
|
return """
|
|
Special Soup: Clam Chowder
|
|
Special Salad: Cobb Salad
|
|
Special Drink: Chai Tea
|
|
"""
|
|
|
|
|
|
@tool(approval_mode="never_require")
|
|
def get_item_price(
|
|
menu_item: Annotated[str, "The name of the menu item."],
|
|
) -> Annotated[str, "Returns the price of the menu item."]:
|
|
return "$9.99"
|
|
|
|
|
|
async def run() -> None:
|
|
# Define an agent
|
|
# Agent's name and description provide better context for AI model
|
|
agent = Agent(
|
|
client=OpenAIChatClient(),
|
|
name="RestaurantAgent",
|
|
description="Answer questions about the menu.",
|
|
tools=[get_specials, get_item_price],
|
|
)
|
|
|
|
# Expose the agent as an MCP server
|
|
server = agent.as_mcp_server()
|
|
|
|
# Run server
|
|
from mcp.server.stdio import stdio_server
|
|
|
|
async def handle_stdin(stdin: Any | None = None, stdout: Any | None = None) -> None:
|
|
async with stdio_server() as (read_stream, write_stream):
|
|
await server.run(read_stream, write_stream, server.create_initialization_options())
|
|
|
|
await handle_stdin()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
anyio.run(run)
|