UNPKG

@code-yeongyu/senpi

Version:

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

128 lines • 5.16 kB
import { encodeMcpHeaderValue } from "./parameter-headers.js"; export const MODERN_PROTOCOL_VERSION = "2026-07-28"; export const MODERN_ERROR_CODES = { HeaderMismatch: -32020, MissingRequiredClientCapabilityError: -32021, UnsupportedProtocolVersionError: -32022, }; export function buildModernRequest(id, method, clientInfo, clientCapabilities, params = {}) { return { jsonrpc: "2.0", id, method, params: { ...params, _meta: { ...params._meta, "io.modelcontextprotocol/protocolVersion": MODERN_PROTOCOL_VERSION, "io.modelcontextprotocol/clientInfo": clientInfo, "io.modelcontextprotocol/clientCapabilities": clientCapabilities, }, }, }; } /** Rewrite an outgoing SDK request or notification into the modern wire shape at the negotiated version. */ export function modernizeRequest(id, method, clientInfo, clientCapabilities, version, params) { const built = buildModernRequest(id, method, clientInfo, clientCapabilities, params); const capabilities = params?._meta?.["io.modelcontextprotocol/clientCapabilities"]; return { ...built, params: { ...built.params, _meta: { ...built.params._meta, "io.modelcontextprotocol/clientCapabilities": isRecord(capabilities) ? capabilities : clientCapabilities, "io.modelcontextprotocol/protocolVersion": version, }, }, }; } /** Derive protocol headers from the same request that will be serialized. */ export function deriveModernHeaders(request) { const headers = { "MCP-Protocol-Version": request.params._meta["io.modelcontextprotocol/protocolVersion"], "Mcp-Method": request.method, }; const name = request.method === "resources/read" ? request.params.uri : request.method === "tools/call" || request.method === "prompts/get" ? request.params.name : ["tasks/get", "tasks/update", "tasks/cancel"].includes(request.method) ? request.params.taskId : undefined; if (typeof name === "string") { headers["Mcp-Name"] = encodeMcpHeaderValue(name); } return headers; } export class ModernWireError extends Error { constructor(message) { super(message); this.name = "ModernWireError"; } } /** Parse the common envelope only; method-specific payloads remain available to their own parsers. */ export function parseModernResult(response) { if (!isRecord(response) || response.jsonrpc !== "2.0" || "error" in response || !isRecord(response.result)) { throw new ModernWireError("Invalid MCP result envelope"); } const result = response.result; if (result.resultType === undefined) return { ...result, resultType: "complete" }; if (typeof result.resultType !== "string") throw new ModernWireError("Invalid MCP resultType"); return { ...result, resultType: result.resultType }; } /** Only the modern-specific codes establish the era, never generic JSON-RPC errors or message text. */ export function classifyModernError(body) { const response = parseBody(body); if (!isRecord(response) || response.jsonrpc !== "2.0" || !isRecord(response.error)) return undefined; const error = response.error; if (typeof error.message !== "string") return undefined; const common = { message: error.message, ...("data" in error ? { data: error.data } : {}) }; switch (error.code) { case MODERN_ERROR_CODES.HeaderMismatch: return { ...common, kind: "HeaderMismatch", code: error.code }; case MODERN_ERROR_CODES.MissingRequiredClientCapabilityError: return { ...common, kind: "MissingRequiredClientCapabilityError", code: error.code }; case MODERN_ERROR_CODES.UnsupportedProtocolVersionError: { const supported = isRecord(error.data) ? error.data.supported : undefined; return { ...common, kind: "UnsupportedProtocolVersionError", code: error.code, ...(Array.isArray(supported) && supported.every((version) => typeof version === "string") ? { supportedVersions: supported } : {}), }; } default: return undefined; } } export function classifyHttpProbeError(status, body) { if (status !== 400) return "indeterminate"; return classifyModernError(body) === undefined ? "legacy" : "modern"; } export function classifyStdioProbeError(body) { return classifyModernError(body) === undefined ? "legacy" : "modern"; } function parseBody(body) { if (typeof body !== "string") return body; try { return JSON.parse(body); } catch (error) { if (error instanceof SyntaxError) return undefined; throw error; } } function isRecord(value) { return typeof value === "object" && value !== null && !Array.isArray(value); } //# sourceMappingURL=wire.js.map