UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

70 lines • 3.09 kB
import { InitializeResultSchema, LATEST_PROTOCOL_VERSION, } from "@modelcontextprotocol/sdk/types.js"; import { ModernTransportError } from "./transport.js"; import { ModernSubscriptions } from "./subscriptions.js"; /** The SDK's local `initialize` answer, built from validated discovery; discovery must support the real wire version. */ export function modernInitializeResult(discovery, wireVersion) { if (!discovery.supportedVersions.includes(wireVersion)) { throw new ModernTransportError(`Discovery does not support negotiated MCP version ${wireVersion}`); } return InitializeResultSchema.parse({ protocolVersion: LATEST_PROTOCOL_VERSION, capabilities: discovery.capabilities, serverInfo: discovery._meta["io.modelcontextprotocol/serverInfo"], ...(discovery.instructions === undefined ? {} : { instructions: discovery.instructions }), }); } /** Modern results may carry `nextCursor: null`, which the SDK's list schemas reject. */ export function normalizeModernResult(message) { if ("result" in message && message.result.nextCursor === null) delete message.result.nextCursor; return message; } /** Local SDK lifecycle dialect; the negotiated wire version belongs only to the underlying transport. */ export class ModernSdkBridge { #transport; #initializeResult; #subscriptions; #initialized = false; constructor(transport, discovery, wireVersion) { this.#initializeResult = modernInitializeResult(discovery, wireVersion); this.#transport = transport; this.#subscriptions = new ModernSubscriptions(transport.send.bind(transport), discovery.capabilities, true); } async start() { this.#transport.onmessage = (message, extra) => { if (this.#subscriptions.incoming(message)) this.onmessage?.(normalizeModernResult(message), extra); }; this.#transport.onclose = () => { this.#subscriptions.dispose(); this.onclose?.(); }; this.#transport.onerror = (error) => this.onerror?.(error); await this.#transport.start(); } async send(message, options) { if ("method" in message && message.method === "initialize") { if (!("id" in message) || this.#initialized) { throw new ModernTransportError("Modern MCP bridge has already initialized"); } this.#initialized = true; this.onmessage?.({ jsonrpc: "2.0", id: message.id, result: this.#initializeResult }); return; } if ("method" in message && message.method === "notifications/initialized" && !("id" in message)) return; this.#subscriptions.outgoing(message); try { await this.#transport.send(message, options); } catch (error) { this.#subscriptions.failedSend(message); throw error; } } close() { this.#subscriptions.dispose(); return this.#transport.close(); } } //# sourceMappingURL=sdk-bridge.js.map