Files
Teamup/tests/TeamUp.IntegrationTests/McpClientTests.cs
T
soroush.asadi c5e0e5cfe3 MCP compatibility for AI agents: server registry, JSON-RPC client, gateway, run-time tool catalog
Agents can now use Model Context Protocol servers. End to end:
- SharedKernel seam IMcpGateway (ListToolsAsync / CallToolAsync) + McpToolDescriptor / McpToolResult,
  so the Assembler discovers and can invoke MCP tools without referencing Integrations' tables.
- Integrations: McpServerConfig (org-scoped, owner-only; auth headers AES-GCM encrypted, never
  returned — only their names) + AddMcpServers migration. McpClient: a dependency-free Streamable-HTTP
  JSON-RPC 2.0 client (initialize → notifications/initialized → tools/list / tools/call), carrying the
  Mcp-Session-Id and parsing both application/json and text/event-stream replies. McpGateway resolves
  an org's servers, decrypts headers server-side, and is best-effort: an unreachable server is logged
  and skipped, never failing the run. CRUD + connectivity-test endpoints (create/test/delete owner-only
  via ManageApiKeys; list via ConfigureAgents to bind).
- OrgBoard: Agent gains McpServerIds (uuid[]; migration backfills existing agents to empty) flowing
  through ConfigureAgent + AgentRunContext.
- Assembler: AgentRunExecutor lists the agent's MCP tools (best-effort) and PromptAssembler renders a
  "# Tools (MCP)" catalog — labelled as data, never instructions — and records it in the run trace.
- Client: SeatsPage gains an MCP servers card (add/test/delete, encrypted auth header) and a per-agent
  MCP server multi-select; api client gains del().

Note: discovery + the governed call gateway are in place now; the autonomous model-driven tool-call
loop (model emits tool_calls → gated execution → feedback) needs a tool-calling model client and is
the next increment — the stub model can't drive it.

Verified: ArchitectureTests 8/8, IntegrationTests 53/53 (McpClientTests: JSON-RPC handshake/session,
json + SSE; McpServerRegistryTests: owner-only, encrypted-header-never-returned, graceful test,
Member 403; PromptAssemblerMcpTests: catalog + trace, omitted when empty), client build green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-13 19:25:43 +03:30

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 + "}";
}
}