mirror of
https://github.com/Kaelio/ktx.git
synced 2026-06-13 08:15:14 +02:00
Address overengineering audit findings across cli/context/connector packages: - F1 Snowflake `query`: drop bare catch that flattened all errors to empty result - F2 memory-agent: treat LLM `stopReason === 'error'` as crash (skip squash-merge) - F3 WikiSearchTool: description honest about token-only fallback vs sqlite-fts5 hybrid - F5 Scan enrichment provider resolution: return discriminated status and surface distinct `llm_unavailable` / `embedding_unavailable` warnings per failure mode - F6 Relationship validation budget: drop dead `tableCount === undefined → 'all'` branch; update tests to pass `tableCount` like production - F8 `ktx sql`: use canonical `resolveOutputMode` (now honors KTX_OUTPUT/CI/TTY) - F9 MCP stdio server: default `protocolIo.stderr` to `process.stderr` so memory_ingest startup failures are visible - F13/F14 Scan/setup JSON readers: distinguish ENOENT from corruption instead of silently treating both as missing - F15 `createKtxCliScanConnector`: throw config-shape error when driver matches but type guard rejects, instead of "no native connector" - F16 ContextEvidenceSearchTool: surface `embedding_unhealthy:<reason>` instead of silently dropping the semantic lane - F17 PromptService: default partials to `[]` (removes stale `clinical_policy` reference from a prior product) - F20 `contextBuildCommands`: drop unused `runId` parameter Dead-code removal: - F4 Delete `AgentRunnerService` (duplicated `RuntimeAgentRunner`, only test-used); migrate tests to exercise `AiSdkKtxLlmRuntime.runAgentLoop` directly - F7 Delete `KtxScanOrchestrator` and its test (no production callers; the inline pipeline in `runLocalScan` is the single source of truth) - F18 Delete `generateKtxText`/`generateKtxObject` pass-through helpers; inline the single `runtime.generateObject` call at its caller Plus a clarifying comment on the SQLite `resolveStringReference` `file:` carve-out (load-bearing for SQLite URI form, not a bug).
64 lines
2.3 KiB
TypeScript
64 lines
2.3 KiB
TypeScript
import process from 'node:process';
|
|
import type { Readable, Writable } from 'node:stream';
|
|
import { loadKtxProject } from '@ktx/context/project';
|
|
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
import type { KtxCliIo } from './cli-runtime.js';
|
|
import { createKtxMcpServerFactory } from './mcp-server-factory.js';
|
|
|
|
export interface RunKtxMcpStdioServerOptions {
|
|
projectDir: string;
|
|
cliVersion?: string;
|
|
io?: KtxCliIo;
|
|
createMcpServer?: () => McpServer;
|
|
loadProject?: typeof loadKtxProject;
|
|
stdin?: Readable;
|
|
stdout?: Writable;
|
|
}
|
|
|
|
export async function runKtxMcpStdioServer(options: RunKtxMcpStdioServerOptions): Promise<void> {
|
|
const project =
|
|
options.createMcpServer === undefined
|
|
? await (options.loadProject ?? loadKtxProject)({ projectDir: options.projectDir })
|
|
: undefined;
|
|
const protocolIo: KtxCliIo = {
|
|
stdout: { write() {} },
|
|
stderr: options.io?.stderr ?? process.stderr,
|
|
};
|
|
const createMcpServer =
|
|
options.createMcpServer ??
|
|
(await createKtxMcpServerFactory({
|
|
project: project!,
|
|
projectDir: options.projectDir,
|
|
cliVersion: options.cliVersion ?? '0.0.0-private',
|
|
io: protocolIo,
|
|
}));
|
|
const stdin = options.stdin ?? process.stdin;
|
|
const transport = new StdioServerTransport(stdin, options.stdout);
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
|
let settled = false;
|
|
const settle = (callback: () => void) => {
|
|
if (settled) return;
|
|
settled = true;
|
|
stdin.off('end', closeTransport);
|
|
stdin.off('close', closeTransport);
|
|
callback();
|
|
};
|
|
const closeTransport = () => {
|
|
transport.close().catch((error: unknown) => {
|
|
settle(() => reject(error instanceof Error ? error : new Error(String(error))));
|
|
});
|
|
};
|
|
transport.onclose = () => settle(resolve);
|
|
transport.onerror = (error) => {
|
|
options.io?.stderr.write(`KTX MCP stdio transport error: ${error.message}\n`);
|
|
settle(() => reject(error));
|
|
};
|
|
stdin.once('end', closeTransport);
|
|
stdin.once('close', closeTransport);
|
|
createMcpServer().connect(transport).catch((error: unknown) => {
|
|
settle(() => reject(error instanceof Error ? error : new Error(String(error))));
|
|
});
|
|
});
|
|
}
|