eust-w b47a07e8d2 chore: baseline snapshot of the Codex-delivered code before review
Imports the previously untracked working tree as-is so that the review
changes that follow (requirements -> design -> implementation) can be
reviewed as diffs and reverted if needed. No content changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 13:18:04 +08:00

25 lines
3.9 KiB
TypeScript

import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
import { z } from 'zod';
import type { Request, Response } from 'express';
import { DomainError, wishInput, releaseInput } from '@wishforge/contracts';
import type { Services } from './http.js';
export async function handleMcp(s: Services, req: Request, res: Response) {
const authenticated = await s.auth.authenticate(req.header('Authorization'));
const actor = { ...authenticated, kind: 'connector' as const };
const server = new McpServer({ name: 'wishforge', version: '0.1.0' }, { instructions: 'Submit a wish with explicit acceptance criteria and reuse its idempotency_key when retrying. Tasks continue after this call returns. Use run_id to inspect status and answer questions. Agent completion is not verified delivery. Human release approvals must be completed at the returned web URL.' });
const result = async (fn: () => Promise<unknown>) => { try { const data = await fn(); return { content: [{ type: 'text' as const, text: JSON.stringify({ data }) }] }; } catch (error) { return { isError: true, content: [{ type: 'text' as const, text: JSON.stringify({ error: { code: error instanceof DomainError ? error.code : 'TOOL_ERROR', message: error instanceof Error ? error.message : 'Tool failed' } }) }] }; } };
server.registerTool('submit_wish', { description: 'Submit a versioned wish; returns quickly with wish_id and run_id.', inputSchema: { project_id: z.string(), idempotency_key: z.string().min(8), ...wishInput.shape } }, ({ project_id, idempotency_key, ...input }) => result(() => s.control.submit(actor, project_id, input, idempotency_key)));
server.registerTool('get_run', { description: 'Read task stage, status and waiting reason.', inputSchema: { run_id: z.string() } }, ({ run_id }) => result(() => s.control.run(actor, run_id)));
server.registerTool('get_run_events', { description: 'Read persisted progress events after a cursor.', inputSchema: { run_id: z.string(), cursor: z.number().int().nonnegative().default(0) } }, ({ run_id, cursor }) => result(() => s.control.events(actor, run_id, cursor)));
server.registerTool('answer_question', { description: 'Add a requirement revision; invalidates obsolete verification and approval.', inputSchema: { run_id: z.string(), answer: z.string().min(1), idempotency_key: z.string().min(8) } }, ({ run_id, answer, idempotency_key }) => result(() => s.control.command(actor, run_id, { action: 'answer', message: answer }, idempotency_key)));
server.registerTool('control_run', { description: 'Pause, resume or cancel future execution. Cancel does not roll back a healthy live release.', inputSchema: { run_id: z.string(), action: z.enum(['pause', 'resume', 'cancel']), idempotency_key: z.string().min(8) } }, ({ run_id, action, idempotency_key }) => result(() => s.control.command(actor, run_id, { action }, idempotency_key)));
server.registerTool('get_delivery', { description: 'Read verified release and evidence, if available.', inputSchema: { run_id: z.string() } }, ({ run_id }) => result(async () => { return {run_id,...await s.releases.delivery(actor,run_id)}; }));
server.registerTool('request_release', { description: 'Request publication of trusted build evidence. High-risk publication requires an interactive human approval.', inputSchema: { ...releaseInput.shape, idempotency_key: z.string().min(8) } }, ({ idempotency_key, ...input }) => result(async () => { const release = await s.releases.request(actor, input, idempotency_key); return { release, approval_url: release.approvalId ? `${s.control.env.WF_PUBLIC_URL}/releases` : null }; }));
const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: undefined, enableJsonResponse: true });
await server.connect(transport);
res.on('close', () => { void transport.close(); void server.close(); });
await transport.handleRequest(req, res, req.body);
}