Files
agent-framework/python/samples/04-hosting/azure_functions/08_mcp_server
T
Copilot b05fc9e849 Python: Add load_dotenv() to samples for .env file support (#4043)
* Initial plan

* Add load_dotenv() to 303 Python samples for environment variable loading

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Update SAMPLE_GUIDELINES.md to document load_dotenv() requirement

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Update samples README.md to document .env file usage

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Run ruff format on all changed sample files

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Clarify load_dotenv() usage in README - local dev vs production

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Remove deprecated getting_started folder as requested

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Document env_file_path parameter for per-client configuration

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Merge main branch to resolve conflicts

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Fix run_evaluation.py file that was empty in merge commit

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Remove dotnet changes from merge - out of scope for this PR

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Remove package and test changes from merge - only sample changes needed

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Remove test_func_utils.py - only sample changes needed

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Revert sample files not in original changeset - keep only load_dotenv additions

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Move load_dotenv() outside snippet tag in 06_host_your_agent.py

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Fix comment placement - move load_dotenv before code comments

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Fix load_dotenv() placement across all samples - after docstring, before code comments

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Merge latest main branch with load_dotenv changes

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Remove non-sample changes from merge - keep only load_dotenv additions

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Revert non-load_dotenv sample changes from merge

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Fix run_evaluation.py - use main's improved version (file already had load_dotenv)

Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>

* Manual update

* Manual update 2

* Fix Role usage and load_dotenv placement per PR review feedback

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Fix Role usage - use string literals not enum attributes

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Fix SAMPLE_GUIDELINES.md example - load_dotenv before docstring per guidance

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Move load_dotenv() before docstrings in all samples per SAMPLE_GUIDELINES ordering

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Address PR review: rename files, fix placement, add session usage, remove note

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

* Update Redis README to reference renamed file redis_history_provider.py

Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com>
Co-authored-by: Tao Chen <taochen@microsoft.com>
Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com>
Co-authored-by: Eduard van Valkenburg <eavanvalkenburg@users.noreply.github.com>
b05fc9e849 · 2026-02-19 10:55:13 +00:00
History
..

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) 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 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:

{
  "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:

    cd python/samples/04-hosting/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
  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:

# 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:

curl http://localhost:7071/api/health

Expected response:

{
  "status": "healthy",
  "agents": [
    {
      "name": "Joker",
      "type": "Agent",
      "http_endpoint_enabled": true,
      "mcp_tool_enabled": false
    },
    {
      "name": "StockAdvisor",
      "type": "Agent",
      "http_endpoint_enabled": false,
      "mcp_tool_enabled": true
    },
    {
      "name": "PlantAdvisor",
      "type": "Agent",
      "http_endpoint_enabled": true,
      "mcp_tool_enabled": true
    }
  ],
  "agent_count": 3
}

Code Structure

The sample shows how to enable MCP tool triggers with flexible agent configuration:

from agent_framework.azure import AgentFunctionApp, AzureOpenAIChatClient

# Create Azure OpenAI Chat Client
client = AzureOpenAIChatClient()

# Define agents with different roles
joker_agent = client.as_agent(
    name="Joker",
    instructions="You are good at telling jokes.",
)

stock_agent = client.as_agent(
    name="StockAdvisor",
    instructions="Check stock prices.",
)

plant_agent = client.as_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