106 lines
4.4 KiB
C#
106 lines
4.4 KiB
C#
|
|
using System.Net;
|
||
|
|
using System.Net.Http.Json;
|
||
|
|
using System.Text;
|
||
|
|
using System.Text.Json;
|
||
|
|
using TeamUp.Modules.Integrations.Mcp;
|
||
|
|
using Xunit;
|
||
|
|
|
||
|
|
namespace TeamUp.IntegrationTests;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// The minimal MCP JSON-RPC client over Streamable HTTP, exercised against a scripted handler: it
|
||
|
|
/// performs the initialize handshake (capturing the session id), lists tools, and calls a tool —
|
||
|
|
/// parsing both an application/json reply and a text/event-stream reply.
|
||
|
|
/// </summary>
|
||
|
|
public sealed class McpClientTests
|
||
|
|
{
|
||
|
|
[Fact]
|
||
|
|
public async Task Lists_tools_and_carries_the_session_id_through_the_handshake()
|
||
|
|
{
|
||
|
|
var handler = new ScriptedMcpHandler();
|
||
|
|
using var http = new HttpClient(handler);
|
||
|
|
var client = new McpClient(http);
|
||
|
|
|
||
|
|
var tools = await client.ListToolsAsync("https://mcp.test/mcp", headers: null);
|
||
|
|
|
||
|
|
Assert.Collection(
|
||
|
|
tools,
|
||
|
|
t => Assert.Equal("search_issues", t.Name),
|
||
|
|
t => Assert.Equal("create_issue", t.Name));
|
||
|
|
Assert.Equal("Search the issue tracker.", tools[0].Description);
|
||
|
|
|
||
|
|
// initialize → notifications/initialized → tools/list, all after the first reply carrying the session.
|
||
|
|
Assert.Equal("initialize", handler.Methods[0]);
|
||
|
|
Assert.Equal("notifications/initialized", handler.Methods[1]);
|
||
|
|
Assert.Equal("tools/list", handler.Methods[2]);
|
||
|
|
Assert.All(handler.SessionIdsAfterInit, id => Assert.Equal("sess-123", id));
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Calls_a_tool_and_extracts_text_from_an_sse_reply()
|
||
|
|
{
|
||
|
|
var handler = new ScriptedMcpHandler();
|
||
|
|
using var http = new HttpClient(handler);
|
||
|
|
var client = new McpClient(http);
|
||
|
|
|
||
|
|
var (success, content, error) = await client.CallToolAsync(
|
||
|
|
"https://mcp.test/mcp", headers: null, toolName: "search_issues", argumentsJson: "{\"q\":\"bug\"}");
|
||
|
|
|
||
|
|
Assert.True(success);
|
||
|
|
Assert.Equal("Found 3 issues.", content);
|
||
|
|
Assert.Null(error);
|
||
|
|
Assert.Equal("tools/call", handler.Methods[^1]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Scripts JSON-RPC replies by method; tools/call answers with an SSE-framed body.</summary>
|
||
|
|
private sealed class ScriptedMcpHandler : HttpMessageHandler
|
||
|
|
{
|
||
|
|
public List<string> Methods { get; } = [];
|
||
|
|
|
||
|
|
public List<string?> SessionIdsAfterInit { get; } = [];
|
||
|
|
|
||
|
|
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
var body = await request.Content!.ReadFromJsonAsync<JsonElement>(cancellationToken);
|
||
|
|
var method = body.GetProperty("method").GetString()!;
|
||
|
|
Methods.Add(method);
|
||
|
|
|
||
|
|
if (method != "initialize")
|
||
|
|
{
|
||
|
|
SessionIdsAfterInit.Add(request.Headers.TryGetValues("Mcp-Session-Id", out var v) ? v.FirstOrDefault() : null);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Notifications get a 202 with no body.
|
||
|
|
if (!body.TryGetProperty("id", out var idElement))
|
||
|
|
{
|
||
|
|
return new HttpResponseMessage(HttpStatusCode.Accepted);
|
||
|
|
}
|
||
|
|
|
||
|
|
var id = idElement.GetInt32();
|
||
|
|
var (payload, sse) = method switch
|
||
|
|
{
|
||
|
|
"initialize" => (Rpc(id, "{\"protocolVersion\":\"2025-06-18\",\"capabilities\":{}}"), false),
|
||
|
|
"tools/list" => (Rpc(id,
|
||
|
|
"{\"tools\":[" +
|
||
|
|
"{\"name\":\"search_issues\",\"description\":\"Search the issue tracker.\",\"inputSchema\":{\"type\":\"object\"}}," +
|
||
|
|
"{\"name\":\"create_issue\",\"description\":\"Open an issue.\",\"inputSchema\":{\"type\":\"object\"}}]}"), false),
|
||
|
|
"tools/call" => (Rpc(id, "{\"content\":[{\"type\":\"text\",\"text\":\"Found 3 issues.\"}],\"isError\":false}"), true),
|
||
|
|
_ => (Rpc(id, "{}"), false),
|
||
|
|
};
|
||
|
|
|
||
|
|
var response = new HttpResponseMessage(HttpStatusCode.OK);
|
||
|
|
if (method == "initialize")
|
||
|
|
{
|
||
|
|
response.Headers.TryAddWithoutValidation("Mcp-Session-Id", "sess-123");
|
||
|
|
}
|
||
|
|
|
||
|
|
response.Content = sse
|
||
|
|
? new StringContent("event: message\ndata: " + payload + "\n\n", Encoding.UTF8, "text/event-stream")
|
||
|
|
: new StringContent(payload, Encoding.UTF8, "application/json");
|
||
|
|
return response;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static string Rpc(int id, string resultJson) => "{\"jsonrpc\":\"2.0\",\"id\":" + id + ",\"result\":" + resultJson + "}";
|
||
|
|
}
|
||
|
|
}
|