feat(#132): port MCP (Model Context Protocol) infrastructure
Implement MCP Phase 1 infrastructure for agent tool integration with central hub, tool registry, and STDIO transport layers. Components: - McpHubService: Central registry for MCP server lifecycle - StdioTransport: STDIO process communication with JSON-RPC 2.0 - ToolRegistryService: Tool catalog management - McpController: REST API for MCP management Endpoints: - GET/POST /mcp/servers - List/register servers - POST /mcp/servers/:id/start|stop - Lifecycle control - DELETE /mcp/servers/:id - Unregister - GET /mcp/tools - List tools - POST /mcp/tools/:name/invoke - Invoke tool Features: - Full JSON-RPC 2.0 protocol support - Process lifecycle management - Buffered message parsing - Type-safe with no explicit any types - Proper cleanup on shutdown Tests: 85 passing with 90.9% coverage Fixes #132 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
118
apps/api/src/mcp/mcp.controller.ts
Normal file
118
apps/api/src/mcp/mcp.controller.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Delete,
|
||||
Param,
|
||||
Body,
|
||||
NotFoundException,
|
||||
BadRequestException,
|
||||
} from "@nestjs/common";
|
||||
import { McpHubService } from "./mcp-hub.service";
|
||||
import { ToolRegistryService } from "./tool-registry.service";
|
||||
import { RegisterServerDto } from "./dto";
|
||||
import type { McpServer, McpTool } from "./interfaces";
|
||||
|
||||
/**
|
||||
* Controller for MCP server and tool management
|
||||
*/
|
||||
@Controller("mcp")
|
||||
export class McpController {
|
||||
constructor(
|
||||
private readonly mcpHub: McpHubService,
|
||||
private readonly toolRegistry: ToolRegistryService
|
||||
) {}
|
||||
|
||||
/**
|
||||
* List all registered MCP servers
|
||||
*/
|
||||
@Get("servers")
|
||||
listServers(): McpServer[] {
|
||||
return this.mcpHub.listServers();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new MCP server
|
||||
*/
|
||||
@Post("servers")
|
||||
async registerServer(@Body() dto: RegisterServerDto): Promise<void> {
|
||||
await this.mcpHub.registerServer(dto);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start an MCP server
|
||||
*/
|
||||
@Post("servers/:id/start")
|
||||
async startServer(@Param("id") id: string): Promise<void> {
|
||||
await this.mcpHub.startServer(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop an MCP server
|
||||
*/
|
||||
@Post("servers/:id/stop")
|
||||
async stopServer(@Param("id") id: string): Promise<void> {
|
||||
await this.mcpHub.stopServer(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister an MCP server
|
||||
*/
|
||||
@Delete("servers/:id")
|
||||
async unregisterServer(@Param("id") id: string): Promise<void> {
|
||||
const server = this.mcpHub.getServerStatus(id);
|
||||
if (!server) {
|
||||
throw new NotFoundException(`Server ${id} not found`);
|
||||
}
|
||||
|
||||
await this.mcpHub.unregisterServer(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* List all available tools
|
||||
*/
|
||||
@Get("tools")
|
||||
listTools(): McpTool[] {
|
||||
return this.toolRegistry.listTools();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific tool by name
|
||||
*/
|
||||
@Get("tools/:name")
|
||||
getTool(@Param("name") name: string): McpTool {
|
||||
const tool = this.toolRegistry.getTool(name);
|
||||
if (!tool) {
|
||||
throw new NotFoundException(`Tool ${name} not found`);
|
||||
}
|
||||
return tool;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoke a tool
|
||||
*/
|
||||
@Post("tools/:name/invoke")
|
||||
async invokeTool(@Param("name") name: string, @Body() input: unknown): Promise<unknown> {
|
||||
const tool = this.toolRegistry.getTool(name);
|
||||
if (!tool) {
|
||||
throw new NotFoundException(`Tool ${name} not found`);
|
||||
}
|
||||
|
||||
const requestId = Math.floor(Math.random() * 1000000);
|
||||
const response = await this.mcpHub.sendRequest(tool.serverId, {
|
||||
jsonrpc: "2.0",
|
||||
id: requestId,
|
||||
method: "tools/call",
|
||||
params: {
|
||||
name: tool.name,
|
||||
arguments: input,
|
||||
},
|
||||
});
|
||||
|
||||
if (response.error) {
|
||||
throw new BadRequestException(response.error.message);
|
||||
}
|
||||
|
||||
return response.result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user