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>
This commit is contained in:
soroush.asadi
2026-06-13 19:25:43 +03:30
parent 0ac15c7308
commit c5e0e5cfe3
27 changed files with 1506 additions and 8 deletions
@@ -15,6 +15,7 @@ public sealed record AgentRunContext(
Guid ApiConfigId,
Guid? FallbackApiConfigId,
IReadOnlyList<string> SkillKeys,
IReadOnlyList<Guid> McpServerIds,
IReadOnlyList<string> Docs,
Guid WorkItemId,
string TaskTitle,
@@ -0,0 +1,24 @@
namespace TeamUp.SharedKernel.Ai;
/// <summary>A tool discovered on one of an org's configured MCP servers.</summary>
public sealed record McpToolDescriptor(Guid ServerId, string ServerName, string Name, string? Description, string InputSchemaJson);
/// <summary>The result of invoking an MCP tool — text content or an error (never throws to the caller).</summary>
public sealed record McpToolResult(bool Success, string? Content, string? Error);
/// <summary>
/// Talks to an org's configured Model Context Protocol servers. Implemented by the Integrations
/// module (which holds the encrypted server credentials); consumed by the Assembler so a run can
/// discover and invoke MCP tools without reaching into Integrations' tables. Discovery is
/// best-effort: a server that fails to connect is skipped, never failing the whole run.
/// </summary>
public interface IMcpGateway
{
/// <summary>Lists the tools exposed by the given (enabled) MCP servers the org owns.</summary>
Task<IReadOnlyList<McpToolDescriptor>> ListToolsAsync(
Guid organizationId, IReadOnlyCollection<Guid> serverIds, CancellationToken cancellationToken = default);
/// <summary>Invokes one tool on one of the org's MCP servers with JSON arguments.</summary>
Task<McpToolResult> CallToolAsync(
Guid organizationId, Guid serverId, string toolName, string argumentsJson, CancellationToken cancellationToken = default);
}