-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Python: Adding support for exposing Agents as MCP tools. #2019
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
318 changes: 268 additions & 50 deletions
318
python/packages/azurefunctions/agent_framework_azurefunctions/_app.py
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
python/samples/getting_started/azure_functions/08_mcp_server/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| # Agent as MCP Tool Sample | ||
|
|
||
| This sample demonstrates how to configure AI agents to be accessible as both HTTP endpoints and [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) tools, enabling flexible integration patterns for AI agent consumption. | ||
|
|
||
| ## Key Concepts Demonstrated | ||
|
|
||
| - **Multi-trigger Agent Configuration**: Configure agents to support HTTP triggers, MCP tool triggers, or both | ||
| - **Microsoft Agent Framework Integration**: Use the framework to define AI agents with specific roles and capabilities | ||
| - **Flexible Agent Registration**: Register agents with customizable trigger configurations | ||
| - **MCP Server Hosting**: Expose agents as MCP tools for consumption by MCP-compatible clients | ||
|
|
||
| ## Sample Architecture | ||
|
|
||
| This sample creates three agents with different trigger configurations: | ||
|
|
||
| | Agent | Role | HTTP Trigger | MCP Tool Trigger | Description | | ||
| |-------|------|--------------|------------------|-------------| | ||
| | **Joker** | Comedy specialist | ✅ Enabled | ❌ Disabled | Accessible only via HTTP requests | | ||
| | **StockAdvisor** | Financial data | ❌ Disabled | ✅ Enabled | Accessible only as MCP tool | | ||
| | **PlantAdvisor** | Indoor plant recommendations | ✅ Enabled | ✅ Enabled | Accessible via both HTTP and MCP | | ||
|
|
||
| ## Environment Setup | ||
|
|
||
| See the [README.md](../README.md) file in the parent directory for complete setup instructions, including: | ||
|
|
||
| - Prerequisites installation | ||
| - Azure OpenAI configuration | ||
| - Durable Task Scheduler setup | ||
| - Storage emulator configuration | ||
|
|
||
| ## Configuration | ||
|
|
||
| Update your `local.settings.json` with your Azure OpenAI credentials: | ||
|
|
||
| ```json | ||
| { | ||
| "Values": { | ||
| "AZURE_OPENAI_ENDPOINT": "https://your-resource.openai.azure.com/", | ||
| "AZURE_OPENAI_CHAT_DEPLOYMENT_NAME": "your-deployment-name", | ||
| "AZURE_OPENAI_KEY": "your-api-key-if-not-using-rbac" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Running the Sample | ||
|
|
||
| 1. **Start the Function App**: | ||
| ```bash | ||
| cd python/samples/getting_started/azure_functions/08_mcp_server | ||
| func start | ||
| ``` | ||
|
|
||
| 2. **Note the MCP Server Endpoint**: When the app starts, you'll see the MCP server endpoint in the terminal output. It will look like: | ||
| ``` | ||
| MCP server endpoint: http://localhost:7071/runtime/webhooks/mcp | ||
| ``` | ||
|
|
||
| ## Testing MCP Tool Integration | ||
|
|
||
| ### Using MCP Inspector | ||
|
|
||
| 1. Install the [MCP Inspector](https://modelcontextprotocol.io/docs/tools/inspector) | ||
| 2. Connect using the MCP server endpoint from your terminal output | ||
| 3. Select **"Streamable HTTP"** as the transport method | ||
| 4. Test the available MCP tools: | ||
| - `StockAdvisor` - Available only as MCP tool | ||
| - `PlantAdvisor` - Available as both HTTP and MCP tool | ||
|
|
||
| ### Using Other MCP Clients | ||
|
|
||
| Any MCP-compatible client can connect to the server endpoint and utilize the exposed agent tools. The agents will appear as callable tools within the MCP protocol. | ||
|
|
||
| ## Testing HTTP Endpoints | ||
|
|
||
| For agents with HTTP triggers enabled (Joker and PlantAdvisor), you can test them using curl: | ||
|
|
||
| ```bash | ||
| # Test Joker agent (HTTP only) | ||
| curl -X POST http://localhost:7071/api/agents/Joker/run \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{"message": "Tell me a joke"}' | ||
|
|
||
| # Test PlantAdvisor agent (HTTP and MCP) | ||
| curl -X POST http://localhost:7071/api/agents/PlantAdvisor/run \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{"message": "Recommend an indoor plant"}' | ||
| ``` | ||
|
|
||
| Note: StockAdvisor does not have HTTP endpoints and is only accessible via MCP tool triggers. | ||
|
|
||
| ## Expected Output | ||
|
|
||
| **HTTP Responses** will be returned directly to your HTTP client. | ||
|
|
||
| **MCP Tool Responses** will be visible in: | ||
| - The terminal where `func start` is running | ||
| - Your MCP client interface | ||
| - The DTS dashboard at `http://localhost:8080` (if using Durable Task Scheduler) | ||
|
|
||
| ## Health Check | ||
|
|
||
| Check the health endpoint to see which agents have which triggers enabled: | ||
|
|
||
| ```bash | ||
| curl http://localhost:7071/api/health | ||
| ``` | ||
|
|
||
| Expected response: | ||
|
|
||
| ```json | ||
| { | ||
| "status": "healthy", | ||
| "agents": [ | ||
| { | ||
| "name": "Joker", | ||
| "type": "Agent", | ||
| "httpEndpointEnabled": true, | ||
| "mcpToolTriggerEnabled": false | ||
| }, | ||
| { | ||
| "name": "StockAdvisor", | ||
| "type": "Agent", | ||
| "httpEndpointEnabled": false, | ||
| "mcpToolTriggerEnabled": true | ||
| }, | ||
| { | ||
| "name": "PlantAdvisor", | ||
| "type": "Agent", | ||
| "httpEndpointEnabled": true, | ||
| "mcpToolTriggerEnabled": true | ||
| } | ||
| ], | ||
| "agent_count": 3 | ||
| } | ||
| ``` | ||
|
|
||
| ## Code Structure | ||
|
|
||
| The sample shows how to enable MCP tool triggers with flexible agent configuration: | ||
|
|
||
| ```python | ||
| from agent_framework.azurefunctions import AgentFunctionApp | ||
| from agent_framework.azure import AzureOpenAIChatClient | ||
|
|
||
| # Create Azure OpenAI Chat Client | ||
| chat_client = AzureOpenAIChatClient() | ||
|
|
||
| # Define agents with different roles | ||
| joker_agent = chat_client.create_agent( | ||
| name="Joker", | ||
| instructions="You are good at telling jokes.", | ||
| ) | ||
|
|
||
| stock_agent = chat_client.create_agent( | ||
| name="StockAdvisor", | ||
| instructions="Check stock prices.", | ||
| ) | ||
|
|
||
| plant_agent = chat_client.create_agent( | ||
| name="PlantAdvisor", | ||
| instructions="Recommend plants.", | ||
| description="Get plant recommendations.", | ||
| ) | ||
|
|
||
| # Create the AgentFunctionApp | ||
| app = AgentFunctionApp(enable_health_check=True) | ||
|
|
||
| # Configure agents with different trigger combinations: | ||
| # HTTP trigger only (default) | ||
| app.add_agent(joker_agent) | ||
|
|
||
| # MCP tool trigger only (HTTP disabled) | ||
| app.add_agent(stock_agent, enable_http_endpoint=False, enable_mcp_tool_trigger=True) | ||
|
|
||
| # Both HTTP and MCP tool triggers enabled | ||
| app.add_agent(plant_agent, enable_http_endpoint=True, enable_mcp_tool_trigger=True) | ||
| ``` | ||
|
|
||
| This automatically creates the following endpoints based on agent configuration: | ||
| - `POST /api/agents/{AgentName}/run` - HTTP endpoint (when `enable_http_endpoint=True`) | ||
| - MCP tool triggers for agents with `enable_mcp_tool_trigger=True` | ||
| - `GET /api/health` - Health check endpoint showing agent configurations | ||
|
|
||
| ## Learn More | ||
|
|
||
| - [Model Context Protocol Documentation](https://modelcontextprotocol.io/) | ||
| - [Microsoft Agent Framework Documentation](https://github.com/Azure/agent-framework) | ||
| - [Azure Functions Documentation](https://learn.microsoft.com/azure/azure-functions/) |
64 changes: 64 additions & 0 deletions
64
python/samples/getting_started/azure_functions/08_mcp_server/function_app.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| """ | ||
| Example showing how to configure AI agents with different trigger configurations. | ||
|
|
||
| This sample demonstrates how to configure agents to be accessible as both HTTP endpoints | ||
| and Model Context Protocol (MCP) tools, enabling flexible integration patterns for AI agent | ||
| consumption. | ||
|
|
||
| Key concepts demonstrated: | ||
| - Multi-trigger Agent Configuration: Configure agents to support HTTP triggers, MCP tool triggers, or both | ||
| - Microsoft Agent Framework Integration: Use the framework to define AI agents with specific roles | ||
| - Flexible Agent Registration: Register agents with customizable trigger configurations | ||
|
|
||
| This sample creates three agents with different trigger configurations: | ||
| - Joker: HTTP trigger only (default) | ||
| - StockAdvisor: MCP tool trigger only (HTTP disabled) | ||
| - PlantAdvisor: Both HTTP and MCP tool triggers enabled | ||
|
|
||
| Required environment variables: | ||
| - AZURE_OPENAI_ENDPOINT: Your Azure OpenAI endpoint | ||
| - AZURE_OPENAI_CHAT_DEPLOYMENT_NAME: Your Azure OpenAI deployment name | ||
|
|
||
| Authentication uses AzureCliCredential (Azure Identity). | ||
| """ | ||
|
|
||
| from agent_framework.azurefunctions import AgentFunctionApp | ||
| from agent_framework.azure import AzureOpenAIChatClient | ||
|
|
||
| # Create Azure OpenAI Chat Client | ||
| # This uses AzureCliCredential for authentication (requires 'az login') | ||
| chat_client = AzureOpenAIChatClient() | ||
|
|
||
| # Define three AI agents with different roles | ||
| # Agent 1: Joker - HTTP trigger only (default) | ||
| agent1 = chat_client.create_agent( | ||
| name="Joker", | ||
| instructions="You are good at telling jokes.", | ||
| ) | ||
|
|
||
| # Agent 2: StockAdvisor - MCP tool trigger only | ||
| agent2 = chat_client.create_agent( | ||
| name="StockAdvisor", | ||
| instructions="Check stock prices.", | ||
| ) | ||
|
|
||
| # Agent 3: PlantAdvisor - Both HTTP and MCP tool triggers | ||
| agent3 = chat_client.create_agent( | ||
| name="PlantAdvisor", | ||
| instructions="Recommend plants.", | ||
| description="Get plant recommendations.", | ||
| ) | ||
|
|
||
| # Create the AgentFunctionApp with selective trigger configuration | ||
| app = AgentFunctionApp( | ||
| enable_health_check=True, | ||
| ) | ||
|
|
||
| # Agent 1: HTTP trigger only (default) | ||
| app.add_agent(agent1) | ||
|
|
||
| # Agent 2: Disable HTTP trigger, enable MCP tool trigger only | ||
| app.add_agent(agent2, enable_http_endpoint=False, enable_mcp_tool_trigger=True) | ||
|
|
||
| # Agent 3: Enable both HTTP and MCP tool triggers | ||
| app.add_agent(agent3, enable_http_endpoint=True, enable_mcp_tool_trigger=True) | ||
7 changes: 7 additions & 0 deletions
7
python/samples/getting_started/azure_functions/08_mcp_server/host.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "version": "2.0", | ||
| "extensionBundle": { | ||
| "id": "Microsoft.Azure.Functions.ExtensionBundle", | ||
| "version": "[4.*, 5.0.0)" | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
python/samples/getting_started/azure_functions/08_mcp_server/local.settings.json.template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "IsEncrypted": false, | ||
| "Values": { | ||
| "FUNCTIONS_WORKER_RUNTIME": "python", | ||
| "AzureWebJobsStorage": "UseDevelopmentStorage=true", | ||
| "DURABLE_TASK_SCHEDULER_CONNECTION_STRING": "Endpoint=http://localhost:8080;TaskHub=default;Authentication=None", | ||
| "AZURE_OPENAI_ENDPOINT": "<AZURE_OPENAI_ENDPOINT>", | ||
| "AZURE_OPENAI_CHAT_DEPLOYMENT_NAME": "<AZURE_OPENAI_CHAT_DEPLOYMENT_NAME>" | ||
| } | ||
| } |
2 changes: 2 additions & 0 deletions
2
python/samples/getting_started/azure_functions/08_mcp_server/requirements.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| agent-framework-azurefunctions | ||
| azure-identity |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sample file is missing the required copyright notice at the top. According to the coding guidelines, all *.py files should have
# Copyright (c) Microsoft. All rights reserved.as the first line, before the docstring.