|
| 1 | +--- |
| 2 | +title: 'MCP tool code samples' |
| 3 | +titleSuffix: Azure AI Foundry |
| 4 | +description: Find code samples to connect Foundry Agent service with MCP. |
| 5 | +services: cognitive-services |
| 6 | +manager: nitinme |
| 7 | +ms.service: azure-ai-agent-service |
| 8 | +ms.topic: how-to |
| 9 | +ms.date: 06/26/2025 |
| 10 | +author: aahill |
| 11 | +ms.author: aahi |
| 12 | +zone_pivot_groups: selection-mcp-code |
| 13 | +ms.custom: azure-ai-agents-code |
| 14 | +--- |
| 15 | + |
| 16 | +# How to connect Foundry Agent service with Model Context Protocol (MCP) |
| 17 | + |
| 18 | +Use this article to find step-by-step instructions and code samples for connecting Foundry Agent service with MCP. |
| 19 | + |
| 20 | +Follow the [REST API Quickstart](../../quickstart.md?pivots=rest-api#api-call-information) to set the right values for the environment variables `AGENT_TOKEN`, `AZURE_AI_FOUNDRY_PROJECT_ENDPOINT` and `API_VERSION`. |
| 21 | + |
| 22 | + |
| 23 | +## Create an Agent with the MCP tool enabled |
| 24 | + |
| 25 | +To make the MCP tool available to your agent, initialize a tool with the server endpoint, server label and more |
| 26 | +```bash |
| 27 | +curl --request POST \ |
| 28 | + --url $AZURE_AI_FOUNDRY_PROJECT_ENDPOINT/assistants?api-version=$API_VERSION \ |
| 29 | + -H "Authorization: Bearer $AGENT_TOKEN" \ |
| 30 | + -H "Content-Type: application/json" \ |
| 31 | + -d "{ |
| 32 | + "instructions": "You are a customer support chatbot. Use the tools provided and your knowledge base to best respond to customer queries.", |
| 33 | + "tools": [ |
| 34 | + { |
| 35 | + "type": "mcp", |
| 36 | + "server_label": "github", |
| 37 | + "server_url": "https://gitmcp.io/Azure/azure-rest-api-specs", |
| 38 | + "require_approval": "never", |
| 39 | + } |
| 40 | + ], |
| 41 | + "name": "my-assistant", |
| 42 | + "model": "gpt-4o", |
| 43 | +}" |
| 44 | +``` |
| 45 | + |
| 46 | +## Create a thread |
| 47 | + |
| 48 | +```bash |
| 49 | +curl --request POST \ |
| 50 | + --url $AZURE_AI_FOUNDRY_PROJECT_ENDPOINT/threads?api-version=$API_VERSION \ |
| 51 | + -H "Authorization: Bearer $AGENT_TOKEN" \ |
| 52 | + -H "Content-Type: application/json" \ |
| 53 | + -d '' |
| 54 | +``` |
| 55 | + |
| 56 | +## Add a user question to the thread |
| 57 | + |
| 58 | +```bash |
| 59 | +curl --request POST \ |
| 60 | + --url $AZURE_AI_FOUNDRY_PROJECT_ENDPOINT/threads/thread_abc123/messages?api-version=$API_VERSION \ |
| 61 | + -H "Authorization: Bearer $AGENT_TOKEN" \ |
| 62 | + -H "Content-Type: application/json" \ |
| 63 | + -d '{ |
| 64 | + "role": "user", |
| 65 | + "content": "<user input related to the MCP server you connect>" |
| 66 | + }' |
| 67 | +``` |
| 68 | + |
| 69 | +## Create a run and check the output |
| 70 | + |
| 71 | +Create a run to pass headers for the tool and observe that the model uses the Grounding with Bing Search tool to provide a response to the user's question. |
| 72 | + |
| 73 | +```bash |
| 74 | +curl --request POST \ |
| 75 | + --url $AZURE_AI_FOUNDRY_PROJECT_ENDPOINT/threads/thread_abc123/runs?api-version=$API_VERSION \ |
| 76 | + -H "Authorization: Bearer $AGENT_TOKEN" \ |
| 77 | + -H "Content-Type: application/json" \ |
| 78 | + -d '{ |
| 79 | + "assistant_id": "<agent_id>", |
| 80 | + "tool_resources": { |
| 81 | + "mcp": [ |
| 82 | + { |
| 83 | + "server_label": "github", |
| 84 | + "headers": { |
| 85 | + "Authorization": "Bearer <token>", |
| 86 | + } |
| 87 | + } |
| 88 | + ] |
| 89 | + }, |
| 90 | + }' |
| 91 | +``` |
| 92 | +## Retrieve the status of the run |
| 93 | + |
| 94 | +```bash |
| 95 | +curl --request GET \ |
| 96 | + --url $AZURE_AI_FOUNDRY_PROJECT_ENDPOINT/threads/thread_abc123/runs/run_abc123?api-version=$API_VERSION \ |
| 97 | + -H "Authorization: Bearer $AGENT_TOKEN" |
| 98 | +``` |
| 99 | + |
| 100 | +## Retrieve the agent response |
| 101 | + |
| 102 | +```bash |
| 103 | +curl --request GET \ |
| 104 | + --url $AZURE_AI_FOUNDRY_PROJECT_ENDPOINT/threads/thread_abc123/messages?api-version=$API_VERSION \ |
| 105 | + -H "Authorization: Bearer $AGENT_TOKEN" |
| 106 | +``` |
0 commit comments