@@ -6,7 +6,7 @@ services: cognitive-services
6
6
manager : nitinme
7
7
ms.service : azure-ai-agent-service
8
8
ms.topic : how-to
9
- ms.date : 07/14 /2025
9
+ ms.date : 08/05 /2025
10
10
author : aahill
11
11
ms.author : aahi
12
12
zone_pivot_groups : selection-mcp-code
@@ -17,6 +17,136 @@ ms.custom: azure-ai-agents-code
17
17
18
18
Use this article to find code samples for connecting Azure AI Foundry Agent Service with Model Context Protocol (MCP) servers.
19
19
20
+ :::zone pivot="csharp"
21
+
22
+ ## Create a project client
23
+
24
+ Create a client object that contains the endpoint for connecting to your AI project and other resources.
25
+
26
+ > [ !NOTE]
27
+ > You can find an asynchronous example on [ GitHub] ( https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/ai/Azure.AI.Agents.Persistent/samples/Sample26_PersistentAgents_MCP.md )
28
+
29
+ ``` csharp
30
+ var projectEndpoint = System .Environment .GetEnvironmentVariable (" PROJECT_ENDPOINT" );
31
+ var modelDeploymentName = System .Environment .GetEnvironmentVariable (" MODEL_DEPLOYMENT_NAME" );
32
+ var mcpServerUrl = System .Environment .GetEnvironmentVariable (" MCP_SERVER_URL" );
33
+ var mcpServerLabel = System .Environment .GetEnvironmentVariable (" MCP_SERVER_LABEL" );
34
+
35
+ PersistentAgentsClient agentClient = new (projectEndpoint , new DefaultAzureCredential ());
36
+
37
+ ```
38
+
39
+ Create the MCP tool definition and configure allowed tools.
40
+
41
+ ``` csharp
42
+ // Create MCP tool definition
43
+ MCPToolDefinition mcpTool = new (mcpServerLabel , mcpServerUrl );
44
+
45
+ // Configure allowed tools (optional)
46
+ string searchApiCode = " search_azure_rest_api_code" ;
47
+ mcpTool .AllowedTools .Add (searchApiCode );
48
+ ```
49
+
50
+ Use the ` MCPToolDefinition ` during the agent initialization.
51
+
52
+ ``` csharp
53
+ PersistentAgent agent = agentClient .Administration .CreateAgent (
54
+ model : modelDeploymentName ,
55
+ name : " my-mcp-agent" ,
56
+ instructions : " You are a helpful agent that can use MCP tools to assist users. Use the available MCP tools to answer questions and perform tasks." ,
57
+ tools : [mcpTool ]);
58
+ ```
59
+
60
+ ## Create a thread and add a message
61
+
62
+ Create the thread, add the message containing a question for agent and start the run with MCP tool resources.
63
+
64
+ ``` csharp
65
+ PersistentAgentThread thread = agentClient .Threads .CreateThread ();
66
+
67
+ // Create message to thread
68
+ PersistentThreadMessage message = agentClient .Messages .CreateMessage (
69
+ thread .Id ,
70
+ MessageRole .User ,
71
+ " Please summarize the Azure REST API specifications Readme" );
72
+
73
+ MCPToolResource mcpToolResource = new (mcpServerLabel );
74
+ mcpToolResource .UpdateHeader (" SuperSecret" , " 123456" );
75
+ ToolResources toolResources = mcpToolResource .ToToolResources ();
76
+
77
+ // Run the agent with MCP tool resources
78
+ ThreadRun run = agentClient .Runs .CreateRun (thread , agent , toolResources );
79
+
80
+ // Handle run execution and tool approvals
81
+ while (run .Status == RunStatus .Queued || run .Status == RunStatus .InProgress || run .Status == RunStatus .RequiresAction )
82
+ {
83
+ Thread .Sleep (TimeSpan .FromMilliseconds (1000 ));
84
+ run = agentClient .Runs .GetRun (thread .Id , run .Id );
85
+
86
+ if (run .Status == RunStatus .RequiresAction && run .RequiredAction is SubmitToolApprovalAction toolApprovalAction )
87
+ {
88
+ var toolApprovals = new List <ToolApproval >();
89
+ foreach (var toolCall in toolApprovalAction .SubmitToolApproval .ToolCalls )
90
+ {
91
+ if (toolCall is RequiredMcpToolCall mcpToolCall )
92
+ {
93
+ Console .WriteLine ($" Approving MCP tool call: {mcpToolCall .Name }, Arguments: {mcpToolCall .Arguments }" );
94
+ toolApprovals .Add (new ToolApproval (mcpToolCall .Id , approve : true )
95
+ {
96
+ Headers = { [" SuperSecret" ] = " 123456" }
97
+ });
98
+ }
99
+ }
100
+
101
+ if (toolApprovals .Count > 0 )
102
+ {
103
+ run = agentClient .Runs .SubmitToolOutputsToRun (thread .Id , run .Id , toolApprovals : toolApprovals );
104
+ }
105
+ }
106
+ }
107
+
108
+ Assert .AreEqual (
109
+ RunStatus .Completed ,
110
+ run .Status ,
111
+ run .LastError ? .Message );
112
+ ```
113
+
114
+ ## Print the messages
115
+
116
+ ``` csharp
117
+ Pageable < PersistentThreadMessage > messages = agentClient .Messages .GetMessages (
118
+ threadId : thread .Id ,
119
+ order : ListSortOrder .Ascending
120
+ );
121
+
122
+ foreach (PersistentThreadMessage threadMessage in messages )
123
+ {
124
+ Console .Write ($" {threadMessage .CreatedAt : yyyy - MM - dd HH : mm : ss } - {threadMessage .Role ,10 }: " );
125
+ foreach (MessageContent contentItem in threadMessage .ContentItems )
126
+ {
127
+ if (contentItem is MessageTextContent textItem )
128
+ {
129
+ Console .Write (textItem .Text );
130
+ }
131
+ else if (contentItem is MessageImageFileContent imageFileItem )
132
+ {
133
+ Console .Write ($" <image from ID: {imageFileItem .FileId }>" );
134
+ }
135
+ Console .WriteLine ();
136
+ }
137
+ }
138
+ ```
139
+
140
+ ## Optional: Delete the agent
141
+
142
+ When you are done with your agent, you can delete it with:
143
+
144
+ ``` csharp
145
+ agentClient .Threads .DeleteThread (threadId : thread .Id );
146
+ agentClient .Administration .DeleteAgent (agentId : agent .Id );
147
+ ```
148
+ ::: zone-end
149
+
20
150
:::zone pivot="python"
21
151
22
152
## Initialize the client
0 commit comments