import { type AnyHarnessTool, approvalId, callChange, type Change, type ConfirmPrompt, failureText, type GatePolicy, type HarnessToolContent, type HarnessToolResult, normalizeToolParams, recordApproval, renderPlan, type ToolCallSuggestion, type ToolFailure } from '@agentic-kit/harness'; import { type CallToolResult, CLIENT_CAPABILITIES_META_KEY, type ClientCapabilities, createRequestStateCodec, inputRequired, type InputRequiredResult, inputResponse, McpServer, type RequestStateCodec, type ServerContext, type ToolAnnotations } from '@modelcontextprotocol/server'; import { serveStdio as serveSdkStdio, type StdioServerHandle, StdioServerTransport } from '@modelcontextprotocol/server/stdio'; import { randomBytes, randomUUID } from 'crypto'; import { Writable } from 'stream'; import { z } from 'zod'; export interface McpToolsOptions { /** The server's name and version, as `initialize` reports them. */ name: string; version: string; tools: readonly AnyHarnessTool[]; /** Makes the plan of each write call, and stops a call that cannot run. */ gate: GatePolicy; /** The project directory the tools act on. */ cwd: string; /** * true serves the tools whose effect is `read`. false serves every tool: one that changes something shows the person * its plan in a form, and runs only when they accept it. */ readOnly: boolean; /** The host's own guidance for the agent (who it acts as, for example), sent after the server's instructions. */ instructions?: string; } const READ_ANNOTATIONS: ToolAnnotations = { readOnlyHint: true }; // MCP reads destructiveHint: false as "only adds" and idempotentHint: true as "a second call changes nothing more". A // write may change or replace what exists (set_access replaces a table's rules), and the tool object promises neither. const CHANGE_ANNOTATIONS: ToolAnnotations = { readOnlyHint: false, destructiveHint: true, idempotentHint: false }; const isServed = (tool: AnyHarnessTool, readOnly: boolean): boolean => tool.effect === 'read' || !readOnly; /** * The schema the SDK checks a call with: the tool's parameters behind the * harness's null rule (null for an optional key that does not take null is not * given). A preprocess step has no JSON Schema of its own, so tools/list shows * the parameters as they are. */ function inputSchemaOf(tool: AnyHarnessTool): z.ZodType, unknown> { if (tool.parameters._zod.def.type !== 'object') { throw new Error(`The tool "${tool.name}" takes parameters that are not an object, and an MCP tool's input must be one. Wrap them in z.object().`); } const parameters = tool.parameters as z.ZodObject; return z.preprocess((value) => normalizeToolParams(parameters, value), parameters); } const toolList = (tools: readonly AnyHarnessTool[]): string => { const names = tools.map((tool) => tool.name); return names.length < 2 ? names.join('') : `${names.slice(0, -1).join(', ')} and ${names[names.length - 1]}`; }; /** What the agent reads when it connects: which tools read and which change things, how a change is approved, then the host's own guidance. */ function serverInstructions(served: readonly AnyHarnessTool[], readOnly: boolean, host: string | undefined): string { const reads = toolList(served.filter((tool) => tool.effect === 'read')); const own = readOnly ? [ `This server serves only tools that read, so nothing changes through it: ${reads}.`, 'For a change, tell the person what to change, so that they can make it themselves.' ] : [ `Call the read tools first, to learn the current state before you change anything: ${reads}.`, `These tools change things: ${toolList(served.filter((tool) => tool.effect !== 'read'))}.`, 'Each call of them shows the person its plan in a form the host shows, and runs only when they accept it, so tell the person what a call will change before you make it.', 'An accepted call runs once, and its result says what changed and gives the plan it applied. A host that cannot show the form gets the plan back instead, for the person to make the change themselves.' ]; return [own.join(' '), host].filter(Boolean).join('\n\n'); } /** What a failed call returns as data: the failure without its calls, which sit beside it. */ type ErrorData = Pick & { code: string }; const errorData = (failure: ErrorData): ErrorData => Object.fromEntries( (['code', 'message', 'hint', 'retryable', 'errorId', 'context'] as const).flatMap((key) => (failure[key] === undefined ? [] : [[key, failure[key]]])) ) as ErrorData; const callsData = (calls: ToolCallSuggestion[] | undefined) => (calls?.length ? { calls } : {}); const content = (parts: HarnessToolContent[]): CallToolResult['content'] => parts.map((part) => ({ ...part })); /** A result of this server: its envelope `{ ok, data?, error?, plan?, calls? }` is always an object. */ type ToolCallResult = CallToolResult & { structuredContent: Record }; function failed(failure: ErrorData & { calls?: ToolCallSuggestion[] }, data?: unknown, parts?: HarnessToolContent[]): ToolCallResult { return { content: parts?.length ? content(parts) : [{ type: 'text', text: failureText(failure as ToolFailure) }], structuredContent: { ok: false, error: errorData(failure), ...(data === undefined ? {} : { data }), ...callsData(failure.calls) }, isError: true }; } function toolResult(result: HarnessToolResult): ToolCallResult { if (result.failure) return failed(result.failure, result.details, result.content); return { content: content(result.content), structuredContent: { ok: true, data: result.details, ...callsData(result.calls) } }; } /** The id the server logged a GraphQL error under, from the errors a thrown client error keeps. */ function errorIdOf(error: unknown): string | undefined { const errors = (error as { errors?: unknown } | null)?.errors; if (!Array.isArray(errors)) return undefined; return errors.map((entry) => (entry as { extensions?: { errorId?: unknown } } | null)?.extensions?.errorId).find((id): id is string => typeof id === 'string'); } /** A tool that threw: its text can carry the server's own words, so the result says only which tool failed, with the logged error's id. */ function thrown(tool: AnyHarnessTool, error: unknown): ToolCallResult { const errorId = errorIdOf(error); const again = tool.effect === 'read' ? 'Call it again once' : 'Read the current state before you call it again, since part of the change may have been made'; const after = errorId ? 'give the error id to the backend operator: it names the logged error' : 'tell the person which tool failed'; return failed({ code: 'failed', message: `The tool ${tool.name} failed with an unexpected error, whose text is not shown.${errorId ? ` Error id: ${errorId}.` : ''}`, hint: `${again}; if it fails again, ${after}.`, ...(errorId ? { errorId } : {}) }); } /** A write's result with the plan it was made from: as text after the tool's own, and as data beside the envelope's. */ const withPlan = (result: ToolCallResult, plan: ConfirmPrompt): ToolCallResult => ({ ...result, content: [...result.content, { type: 'text', text: `The plan of this call:\n${renderPlan(plan)}` }], structuredContent: { ...result.structuredContent, plan } }); const APPLY = 'apply'; // The form is the approval: accepting it as shown applies the change, and unticking the box or declining changes nothing. const APPLY_FIELD = { type: 'boolean', title: 'Apply this change', default: true } as const; type PreviewAnswer = { action: 'accept' | 'decline' | 'cancel'; apply: boolean }; const NOT_APPLIED: Record = { accept: 'The person unticked "Apply this change" when its plan was shown, so nothing was changed.', decline: 'The person declined the change when its plan was shown, so nothing was changed.', cancel: 'The plan was closed without an answer, so nothing was changed.' }; // What the preview's requestState carries back: the id of the plan the person was shown, signed by the server. type Shown = { approval: string }; const CHANGED = 'The plan changed after it was shown, so this is the new plan.'; const NO_FORM = 'This host cannot show the plan of a change for the person to approve, so nothing changed.'; async function previewQuestion(plan: ConfirmPrompt, approval: string, codec: RequestStateCodec, changed = false): Promise { return inputRequired({ requestState: await codec.mint({ approval }), inputRequests: { [APPLY]: inputRequired.elicit({ mode: 'form', message: changed ? `${CHANGED}\n\n${renderPlan(plan)}` : renderPlan(plan), requestedSchema: { type: 'object', properties: { [APPLY]: APPLY_FIELD }, required: [APPLY] } }) } }); } function previewAnswer(ctx: ServerContext): PreviewAnswer | null { const view = inputResponse(ctx.mcpReq.inputResponses, APPLY); if (view.kind !== 'elicit') return null; return { action: view.action, apply: view.action === 'accept' && (view.content?.[APPLY] ?? APPLY_FIELD.default) === true }; } /** The client's capabilities for this request: its envelope on the 2026 era, its `initialize` on a 2025-era connection. */ function clientCapabilities(server: McpServer, ctx: ServerContext): ClientCapabilities | undefined { const envelope = ctx.mcpReq.envelope as Record | undefined; return envelope ? (envelope[CLIENT_CAPABILITIES_META_KEY] as ClientCapabilities | undefined) : server.server.getClientCapabilities(); } // A bare `elicitation: {}` is the 2025 form of declaring form mode, as the SDK reads it. function supportsFormElicitation(capabilities: ClientCapabilities | undefined): boolean { const elicitation = capabilities?.elicitation as { form?: unknown; url?: unknown } | undefined; return elicitation !== undefined && (elicitation.form !== undefined || elicitation.url === undefined); } /** * A read runs. For a change, the gate makes its plan first and stops a call that * cannot run. The plan is shown to the person in a form, with its approval id * signed in the request state: a decline there changes nothing, and an accepted * plan applies only while the plan made again on the answer is the same one, * which the tool checks once more at apply. A changed plan is shown again. A * client that cannot show a form gets the plan back and nothing changes, since * no one could approve it. */ function handlerFor(tool: AnyHarnessTool, server: McpServer, options: McpToolsOptions, codec: RequestStateCodec) { return async (params: Record, ctx: ServerContext): Promise => { const run = async (approved?: string): Promise => { const toolCallId = approved === undefined ? undefined : randomUUID(); if (toolCallId) recordApproval(toolCallId, options.cwd, { id: approved, gate: options.gate }); try { return toolResult(await tool.execute(params, { cwd: options.cwd, ...(toolCallId ? { toolCallId } : {}), signal: ctx.mcpReq.signal })); } catch (error) { return thrown(tool, error); } }; let change: Change; try { change = await callChange(tool, params, options.gate, options.cwd); } catch (error) { return thrown(tool, error); } if (change.effect === 'read') return run(); if (change.decision.kind === 'blocked') return failed(change.decision.failure); const plan = change.decision.prompt; if (supportsFormElicitation(clientCapabilities(server, ctx))) { const approval = approvalId({ action: tool.name, input: change.decision.input, cwd: options.cwd, plan }); const answer = previewAnswer(ctx); if (!answer) return previewQuestion(plan, approval, codec); if (!answer.apply) { const hint = 'Do not make this change again unless the person asks for it.'; return withPlan(failed({ code: 'cancelled', message: NOT_APPLIED[answer.action], hint }), plan); } if (ctx.mcpReq.requestState()?.approval !== approval) return previewQuestion(plan, approval, codec, true); return withPlan(await run(approval), plan); } return withPlan(failed({ code: 'unsupported', message: NO_FORM, hint: 'Tell the person this plan, so that they can make the change themselves.' }), plan); }; } /** A factory of MCP servers that serve `tools`, one server per connection (or per request on HTTP). */ export function createMcpServerFactory(options: McpToolsOptions): () => McpServer { const served = options.tools.filter((tool) => isServed(tool, options.readOnly)); const schemas = served.map(inputSchemaOf); const instructions = serverInstructions(served, options.readOnly, options.instructions); // One key for every server this factory makes, so a preview minted by one can be answered on another (HTTP). const codec = createRequestStateCodec({ key: randomBytes(32) }); return () => { const server = new McpServer( { name: options.name, version: options.version }, { capabilities: { tools: {} }, instructions, requestState: { verify: codec.verify } } ); served.forEach((tool, index) => { server.registerTool( tool.name, { title: tool.label, description: tool.description, annotations: tool.effect === 'read' ? READ_ANNOTATIONS : CHANGE_ANNOTATIONS, inputSchema: schemas[index] }, handlerFor(tool, server, options, codec) ); }); return server; }; } /** * The process's stdout, for the protocol alone. Every other write to * `process.stdout` (console.log among them) goes to stderr from here on. A * child process that inherits file descriptor 1 still writes past this. */ function protocolStdout(): Writable { const stdout = process.stdout; const write = stdout.write.bind(stdout) as (chunk: unknown, encoding: BufferEncoding, callback: (error?: Error | null) => void) => boolean; stdout.write = process.stderr.write.bind(process.stderr) as typeof stdout.write; const protocol = new Writable({ write(chunk, encoding, callback) { write(chunk, encoding, callback); } }); stdout.on('error', (error) => protocol.destroy(error)); return protocol; } /** A server on this process's stdio. `closed` settles when the client closes stdin or `close()` ends the connection. */ export interface McpStdioServer { close(): Promise; closed: Promise; } /** Serves `tools` over this process's stdin and stdout, on both protocol eras. */ export function serveStdio(options: McpToolsOptions): McpStdioServer { let ended: () => void; const closed = new Promise((resolve) => { ended = resolve; }); process.stdin.once('end', () => ended()).once('close', () => ended()); const handle: StdioServerHandle = serveSdkStdio(createMcpServerFactory(options), { transport: new StdioServerTransport(process.stdin, protocolStdout()) }); return { close: async () => { await handle.close(); ended(); }, closed }; }