@@ -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,133 @@ 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
40
+
41
+ Create the MCP tool definition and configure allowed tools.
42
+
43
+ ``` csharp
44
+ // Create MCP tool definition
45
+ MCPToolDefinition mcpTool = new (mcpServerLabel , mcpServerUrl );
46
+
47
+ // Configure allowed tools (optional)
48
+ string searchApiCode = " search_azure_rest_api_code" ;
49
+ mcpTool .AllowedTools .Add (searchApiCode );
50
+ ```
51
+
52
+ Use the ` MCPToolDefinition ` during the agent initialization.
53
+
54
+ ``` csharp
55
+ PersistentAgent agent = agentClient .Administration .CreateAgent (
56
+ model : modelDeploymentName ,
57
+ name : " my-mcp-agent" ,
58
+ 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." ,
59
+ tools : [mcpTool ]);
60
+ ```
61
+
62
+ ## Create a thread and add a message
63
+
64
+ Create the thread, add the message containing a question for agent and start the run with MCP tool resources.
65
+
66
+ ``` csharp
67
+ PersistentAgentThread thread = agentClient .Threads .CreateThread ();
68
+
69
+ // Create message to thread
70
+ PersistentThreadMessage message = agentClient .Messages .CreateMessage (
71
+ thread .Id ,
72
+ MessageRole .User ,
73
+ " Please summarize the Azure REST API specifications Readme" );
74
+
75
+ MCPToolResource mcpToolResource = new (mcpServerLabel );
76
+ mcpToolResource .UpdateHeader (" SuperSecret" , " 123456" );
77
+ ToolResources toolResources = mcpToolResource .ToToolResources ();
78
+
79
+ // Run the agent with MCP tool resources
80
+ ThreadRun run = agentClient .Runs .CreateRun (thread , agent , toolResources );
81
+
82
+ // Handle run execution and tool approvals
83
+ while (run .Status == RunStatus .Queued || run .Status == RunStatus .InProgress || run .Status == RunStatus .RequiresAction )
84
+ {
85
+ Thread .Sleep (TimeSpan .FromMilliseconds (1000 ));
86
+ run = agentClient .Runs .GetRun (thread .Id , run .Id );
87
+
88
+ if (run .Status == RunStatus .RequiresAction && run .RequiredAction is SubmitToolApprovalAction toolApprovalAction )
89
+ {
90
+ var toolApprovals = new List <ToolApproval >();
91
+ foreach (var toolCall in toolApprovalAction .SubmitToolApproval .ToolCalls )
92
+ {
93
+ if (toolCall is RequiredMcpToolCall mcpToolCall )
94
+ {
95
+ Console .WriteLine ($" Approving MCP tool call: {mcpToolCall .Name }, Arguments: {mcpToolCall .Arguments }" );
96
+ toolApprovals .Add (new ToolApproval (mcpToolCall .Id , approve : true )
97
+ {
98
+ Headers = { [" SuperSecret" ] = " 123456" }
99
+ });
100
+ }
101
+ }
102
+
103
+ if (toolApprovals .Count > 0 )
104
+ {
105
+ run = agentClient .Runs .SubmitToolOutputsToRun (thread .Id , run .Id , toolApprovals : toolApprovals );
106
+ }
107
+ }
108
+ }
109
+ ```
110
+
111
+ ## Print the messages
112
+
113
+ ``` csharp
114
+ Pageable < PersistentThreadMessage > messages = agentClient .Messages .GetMessages (
115
+ threadId : thread .Id ,
116
+ order : ListSortOrder .Ascending
117
+ );
118
+
119
+ foreach (PersistentThreadMessage threadMessage in messages )
120
+ {
121
+ Console .Write ($" {threadMessage .CreatedAt : yyyy - MM - dd HH : mm : ss } - {threadMessage .Role ,10 }: " );
122
+ foreach (MessageContent contentItem in threadMessage .ContentItems )
123
+ {
124
+ if (contentItem is MessageTextContent textItem )
125
+ {
126
+ Console .Write (textItem .Text );
127
+ }
128
+ else if (contentItem is MessageImageFileContent imageFileItem )
129
+ {
130
+ Console .Write ($" <image from ID: {imageFileItem .FileId }>" );
131
+ }
132
+ Console .WriteLine ();
133
+ }
134
+ }
135
+ ```
136
+
137
+ ## Optional: Delete the agent
138
+
139
+ When you are done with your agent, you can delete it with:
140
+
141
+ ``` csharp
142
+ agentClient .Threads .DeleteThread (threadId : thread .Id );
143
+ agentClient .Administration .DeleteAgent (agentId : agent .Id );
144
+ ```
145
+ ::: zone-end
146
+
20
147
:::zone pivot="python"
21
148
22
149
## Initialize the client
0 commit comments