feat(api): MCP server endpoint — expose AI tools to external clients

Mount an MCP (Model Context Protocol) server at /api/v1/mcp in the
unified Hono API. External clients like Claude Desktop, Cursor, and
VS Code Copilot can discover and call all 29 Mana tools via the
standard MCP protocol.

Architecture:
- WebStandardStreamableHTTPServerTransport for Bun/Hono compatibility
- AI_TOOL_CATALOG → MCP tool definitions with JSON Schema (via Zod)
- Stateful sessions with Mcp-Session-Id header
- Auth via existing authMiddleware (JWT or API key)

Phase 1 scope: tools/list returns all 29 tools with schemas,
tools/call acknowledges with descriptive messages. Phase 2 will add
actual DB reads/writes via sync_changes.

Usage:
  Claude Desktop config:
  {"mcpServers": {"mana": {"url": "http://localhost:3060/api/v1/mcp"}}}

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Till JS 2026-04-16 13:37:52 +02:00
parent 827b252090
commit db4dd437bd
6 changed files with 508 additions and 5 deletions

View file

@ -0,0 +1,67 @@
/**
* MCP Tool Executor handles tools/call requests by routing to module
* handlers or returning status messages.
*
* Phase 1: Read-only tools query the sync database directly.
* Phase 2: Write tools will insert into sync_changes (like mana-ai does).
*/
import { AI_TOOL_CATALOG_BY_NAME } from '@mana/shared-ai';
export interface McpToolResult {
[key: string]: unknown;
content: Array<{ type: 'text'; text: string }>;
isError?: boolean;
}
/**
* Execute an MCP tool call. Returns MCP-formatted result content.
*
* Phase 1 scope:
* - All tools are listed (via tools/list from AI_TOOL_CATALOG)
* - Write tools return a "coming soon" message
* - Read tools are planned for Phase 2 (requires sync DB queries)
*/
export async function executeMcpTool(
toolName: string,
args: Record<string, unknown>,
_userId: string
): Promise<McpToolResult> {
const schema = AI_TOOL_CATALOG_BY_NAME.get(toolName);
if (!schema) {
return {
content: [{ type: 'text', text: `Unknown tool: ${toolName}` }],
isError: true,
};
}
// Phase 1: all tools return a descriptive message about what they will do.
// Phase 2 will implement actual DB reads and sync_changes writes.
if (schema.defaultPolicy === 'auto') {
return {
content: [
{
type: 'text',
text:
`[Mana MCP] Read-tool "${toolName}" (${schema.module}) acknowledged.\n` +
`Args: ${JSON.stringify(args)}\n` +
`Note: Server-side execution coming in Phase 2. ` +
`This tool will query the sync database for user data.`,
},
],
};
}
return {
content: [
{
type: 'text',
text:
`[Mana MCP] Write-tool "${toolName}" (${schema.module}) acknowledged.\n` +
`Args: ${JSON.stringify(args)}\n` +
`Note: Server-side execution coming in Phase 2. ` +
`This tool will write to the sync database and appear on your devices.`,
},
],
};
}