-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathMcpAcceptanceTestSupport.cs
More file actions
194 lines (165 loc) · 7.07 KB
/
McpAcceptanceTestSupport.cs
File metadata and controls
194 lines (165 loc) · 7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
namespace ServiceControl.AcceptanceTesting.Mcp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using System.Threading.Tasks;
using NUnit.Framework;
public static class McpAcceptanceTestSupport
{
const string RequestedProtocolVersion = "2025-11-25";
public static async Task<HttpResponseMessage> InitializeMcpSession(HttpClient httpClient)
{
var request = new HttpRequestMessage(HttpMethod.Post, "/mcp")
{
Content = JsonContent.Create(new
{
jsonrpc = "2.0",
id = 1,
method = "initialize",
@params = new
{
protocolVersion = RequestedProtocolVersion,
capabilities = new { },
clientInfo = new { name = "test-client", version = "1.0" }
}
})
};
request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("text/event-stream"));
request.Headers.Add("MCP-Protocol-Version", RequestedProtocolVersion);
return await httpClient.SendAsync(request);
}
public static async Task<McpSessionInfo> InitializeAndGetSessionInfo(HttpClient httpClient)
{
var response = await InitializeMcpSession(httpClient);
if (!response.IsSuccessStatusCode)
{
return null;
}
var initializeResponse = JsonSerializer.Deserialize(await ReadMcpResponseJson(response), McpAcceptanceJsonContext.Default.McpInitializeResponse)!;
var protocolVersion = initializeResponse.Result.ProtocolVersion;
if (!response.Headers.TryGetValues("mcp-session-id", out var values))
{
return null;
}
var sessionId = values.FirstOrDefault();
if (sessionId == null)
{
return null;
}
var initializedResponse = await SendInitializedNotification(httpClient, sessionId, protocolVersion);
if (!initializedResponse.IsSuccessStatusCode)
{
return null;
}
return new McpSessionInfo(sessionId, protocolVersion);
}
public static async Task<HttpResponseMessage> SendMcpRequest(HttpClient httpClient, McpSessionInfo sessionInfo, string method, object @params)
{
var request = new HttpRequestMessage(HttpMethod.Post, "/mcp")
{
Content = JsonContent.Create(new
{
jsonrpc = "2.0",
id = 2,
method,
@params
})
};
request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("text/event-stream"));
request.Headers.Add("mcp-session-id", sessionInfo.SessionId);
request.Headers.Add("MCP-Protocol-Version", sessionInfo.ProtocolVersion);
return await httpClient.SendAsync(request);
}
public static async Task<string> ReadMcpResponseJson(HttpResponseMessage response)
{
var body = await response.Content.ReadAsStringAsync();
var contentType = response.Content.Headers.ContentType?.MediaType;
if (contentType == "text/event-stream")
{
foreach (var line in body.Split('\n'))
{
if (line.StartsWith("data: "))
{
return line.Substring("data: ".Length);
}
}
}
return body;
}
public static McpListToolsResponse DeserializeListToolsResponse(string toolsJson) =>
JsonSerializer.Deserialize(toolsJson, McpAcceptanceJsonContext.Default.McpListToolsResponse)!;
public static McpCallToolResponse DeserializeCallToolResponse(string toolResult) =>
JsonSerializer.Deserialize(toolResult, McpAcceptanceJsonContext.Default.McpCallToolResponse)!;
public static string FormatToolsForApproval(List<JsonElement> sortedTools) =>
JsonSerializer.Serialize(sortedTools, McpAcceptanceJsonContext.Default.ListJsonElement);
public static void AssertToolsHaveOutputSchema(IEnumerable<JsonElement> tools)
{
foreach (var tool in tools)
{
Assert.That(tool.TryGetProperty("outputSchema", out var outputSchema), Is.True, $"Tool '{tool.GetProperty("name").GetString()}' should expose outputSchema.");
Assert.That(outputSchema.ValueKind, Is.EqualTo(JsonValueKind.Object), $"Tool '{tool.GetProperty("name").GetString()}' should expose object outputSchema.");
}
}
public static void AssertStructuredToolResponse(string rawResponse, JsonElement structuredContent, IReadOnlyList<McpContent> content, Action<JsonElement> assertStructuredContent)
{
Assert.That(structuredContent.ValueKind, Is.EqualTo(JsonValueKind.Object), rawResponse);
assertStructuredContent(structuredContent);
Assert.That(content, Has.Count.GreaterThanOrEqualTo(1), rawResponse);
Assert.That(content[0].Type, Is.EqualTo("text"), rawResponse);
Assert.That(content[0].Text, Is.Not.Null.And.Not.Empty, rawResponse);
using var textPayload = JsonDocument.Parse(content[0].Text);
Assert.That(JsonElement.DeepEquals(structuredContent, textPayload.RootElement), Is.True, $"text content should serialize the structured payload. Raw response: {rawResponse}");
}
static async Task<HttpResponseMessage> SendInitializedNotification(HttpClient httpClient, string sessionId, string protocolVersion)
{
var request = new HttpRequestMessage(HttpMethod.Post, "/mcp")
{
Content = JsonContent.Create(new
{
jsonrpc = "2.0",
method = "notifications/initialized"
})
};
request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("text/event-stream"));
request.Headers.Add("mcp-session-id", sessionId);
request.Headers.Add("MCP-Protocol-Version", protocolVersion);
return await httpClient.SendAsync(request);
}
}
public record McpSessionInfo(string SessionId, string ProtocolVersion);
public class McpListToolsResponse
{
public McpListToolsResult Result { get; set; }
}
public class McpListToolsResult
{
public List<object> Tools { get; set; } = [];
}
public class McpCallToolResponse
{
public McpCallToolResult Result { get; set; }
}
public class McpCallToolResult
{
public JsonElement StructuredContent { get; set; }
public List<McpContent> Content { get; set; } = [];
}
public class McpContent
{
public string Type { get; set; }
public string Text { get; set; }
}
public class McpInitializeResponse
{
public McpInitializeResult Result { get; set; }
}
public class McpInitializeResult
{
public string ProtocolVersion { get; set; }
}