@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
107 lines • 4.95 kB
JavaScript
import { Buffer } from "node:buffer";
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
/** RFC 9110 field values, preserving whitespace and literal sentinel strings. */
export function encodeMcpHeaderValue(value) {
return /^[\x09\x20-\x7e]*$/.test(value) &&
value.trim() === value &&
!(value.startsWith("=?base64?") && value.endsWith("?="))
? value
: `=?base64?${Buffer.from(value, "utf8").toString("base64")}?=`;
}
/** Configured headers must never supply parameter metadata absent from the request body. */
export function clearMcpParameterHeaders(headers) {
for (const name of [...headers.keys()]) {
if (name.toLowerCase().startsWith("mcp-param-"))
headers.delete(name);
}
}
function parseParameterHeaders(schema) {
const headers = [];
const names = new Set();
let reason = "";
const visit = (node, path) => {
if (Array.isArray(node))
return node.every((child) => visit(child, undefined));
if (!isRecord(node))
return true;
if (Object.hasOwn(node, "x-mcp-header")) {
const name = node["x-mcp-header"];
const type = node.type;
if (!path?.length)
reason = "x-mcp-header must be on a properties-only path";
else if (typeof name !== "string" || !/^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/.test(name))
reason = "x-mcp-header must be a nonempty HTTP field-name token";
else if (names.has(name.toLowerCase()))
reason = "x-mcp-header names must be case-insensitively unique";
else if (type !== "string" && type !== "integer" && type !== "boolean")
reason = "x-mcp-header type must be string, integer, or boolean";
else {
names.add(name.toLowerCase());
headers.push({ name, path, type });
}
if (reason)
return false;
}
// "Anywhere else" includes unknown keywords; instance-data keywords are not schemas.
for (const [keyword, value] of Object.entries(node)) {
if (["properties", "patternProperties", "$defs", "definitions", "dependentSchemas", "dependencies"].includes(keyword)) {
if (!isRecord(value))
continue;
for (const [key, child] of Object.entries(value)) {
const childPath = keyword === "properties" && path !== undefined ? [...path, key] : undefined;
if (!visit(child, childPath))
return false;
}
}
else if (!["examples", "default", "const", "enum"].includes(keyword)) {
if (!visit(value, undefined))
return false;
}
}
return true;
};
return visit(schema, []) ? headers : reason;
}
/** A local argument error, not a failed transport or an untrusted server diagnostic. */
export class ParameterHeaderValidationError extends Error {
}
/** Connection-local catalog: never shared with another server or transport generation. */
export class ModernParameterHeaders {
#tools = new Map();
filterTools(tools, firstPage, warn) {
if (firstPage)
this.#tools.clear();
return tools.filter((tool) => {
if (!isRecord(tool) || typeof tool.name !== "string")
return true;
const headers = parseParameterHeaders(tool.inputSchema);
if (typeof headers === "string") {
this.#tools.delete(tool.name);
warn(`Rejected MCP tool ${JSON.stringify(tool.name)}: ${headers}`);
return false;
}
this.#tools.set(tool.name, headers);
return true;
});
}
append(headers, name, args) {
if (typeof name !== "string")
return;
for (const parameter of this.#tools.get(name) ?? []) {
let value = args;
for (const key of parameter.path) {
value = isRecord(value) && Object.hasOwn(value, key) ? value[key] : undefined;
}
if (value === undefined)
continue;
const valid = (parameter.type === "string" && typeof value === "string") ||
(parameter.type === "boolean" && typeof value === "boolean") ||
(parameter.type === "integer" && typeof value === "number" && Number.isSafeInteger(value));
if (!valid) {
throw new ParameterHeaderValidationError(`MCP tool ${JSON.stringify(name)} argument ${JSON.stringify(parameter.path)} must be ${parameter.type === "integer" ? "a safe integer" : `a ${parameter.type}`}`);
}
headers.set(`Mcp-Param-${parameter.name}`, encodeMcpHeaderValue(String(value)));
}
}
}
//# sourceMappingURL=parameter-headers.js.map