codebase-snapshot 0.20.12 → 0.20.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/README.md +8 -3
- package/dist/bin.js +272 -56
- package/dist/{chunk-TUKY6KLH.js → chunk-GNVRD7XS.js} +274 -58
- package/dist/cli.cjs +272 -56
- package/dist/cli.js +2 -2
- package/dist/index.cjs +281 -57
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/mcp.cjs +260 -44
- package/dist/mcp.d.cts +13 -4
- package/dist/mcp.d.ts +13 -4
- package/dist/mcp.js +1 -1
- package/docs/cli.md +9 -8
- package/docs/configuration.md +1 -1
- package/docs/deploy.md +2 -2
- package/docs/mcp.md +47 -25
- package/docs/migration.md +7 -2
- package/docs/security-review.md +12 -1
- package/package.json +2 -1
|
@@ -4189,7 +4189,7 @@ function verifySnapshotSignature(snapshotDir) {
|
|
|
4189
4189
|
|
|
4190
4190
|
// src/version.ts
|
|
4191
4191
|
var GENERATOR_NAME = "codebase-snapshot";
|
|
4192
|
-
var GENERATOR_VERSION = "0.20.
|
|
4192
|
+
var GENERATOR_VERSION = "0.20.13";
|
|
4193
4193
|
var SCHEMA_VERSION = "3.0.0";
|
|
4194
4194
|
var LEGACY_GENERATOR_NAMES = ["@ankurhalder/codebase-snapshot"];
|
|
4195
4195
|
function isOurGeneratorName(name) {
|
|
@@ -7241,7 +7241,7 @@ function renumberParts(parts, contentFingerprint) {
|
|
|
7241
7241
|
}
|
|
7242
7242
|
const pathLines = included.map((file) => displayPath(file.relativePath));
|
|
7243
7243
|
const listFence = fenceForText(pathLines.join("\n"), 3);
|
|
7244
|
-
const
|
|
7244
|
+
const header2 = [
|
|
7245
7245
|
`# Codebase Snapshot Part ${zeroPad(index + 1)} of ${zeroPad(total)}`,
|
|
7246
7246
|
"",
|
|
7247
7247
|
`- Previous: ${index === 0 ? "none" : nameFor(index)}`,
|
|
@@ -7262,7 +7262,7 @@ function renumberParts(parts, contentFingerprint) {
|
|
|
7262
7262
|
"## Contents",
|
|
7263
7263
|
""
|
|
7264
7264
|
].join("\n");
|
|
7265
|
-
const assembled = (
|
|
7265
|
+
const assembled = (header2 + body).replace(/\r\n/g, "\n");
|
|
7266
7266
|
const estimatedTokens = estimateTokens(assembled);
|
|
7267
7267
|
const markdown = insertTokenLine(assembled, estimatedTokens);
|
|
7268
7268
|
const metadata = {
|
|
@@ -7448,7 +7448,7 @@ function assembleGroup(group, index, total, contentFingerprint) {
|
|
|
7448
7448
|
}
|
|
7449
7449
|
const pathLines = group.sections.map((s) => displayPath(s.file.relativePath));
|
|
7450
7450
|
const listFence = fenceForText(pathLines.join("\n"), 3);
|
|
7451
|
-
const
|
|
7451
|
+
const header2 = [
|
|
7452
7452
|
`# Codebase Snapshot Part ${zeroPad(index + 1)} of ${zeroPad(total)}`,
|
|
7453
7453
|
"",
|
|
7454
7454
|
`- Previous: ${prev}`,
|
|
@@ -7469,7 +7469,7 @@ function assembleGroup(group, index, total, contentFingerprint) {
|
|
|
7469
7469
|
"## Contents",
|
|
7470
7470
|
""
|
|
7471
7471
|
].join("\n");
|
|
7472
|
-
const assembled = (
|
|
7472
|
+
const assembled = (header2 + group.sections.map((s) => s.text).join("")).replace(
|
|
7473
7473
|
/\r\n|\r/g,
|
|
7474
7474
|
"\n"
|
|
7475
7475
|
);
|
|
@@ -8166,10 +8166,10 @@ function headerPaths(value) {
|
|
|
8166
8166
|
}
|
|
8167
8167
|
function diffSectionPaths(body) {
|
|
8168
8168
|
const paths = /* @__PURE__ */ new Set();
|
|
8169
|
-
let
|
|
8169
|
+
let header2 = null;
|
|
8170
8170
|
for (const line of body.split("\n")) {
|
|
8171
8171
|
if (line.startsWith("diff --git ")) {
|
|
8172
|
-
|
|
8172
|
+
header2 = line.slice("diff --git ".length);
|
|
8173
8173
|
continue;
|
|
8174
8174
|
}
|
|
8175
8175
|
if (line.startsWith("+++ ")) {
|
|
@@ -8182,8 +8182,8 @@ function diffSectionPaths(body) {
|
|
|
8182
8182
|
if (path27 !== null) paths.add(path27);
|
|
8183
8183
|
}
|
|
8184
8184
|
}
|
|
8185
|
-
if (paths.size === 0 &&
|
|
8186
|
-
for (const path27 of headerPaths(
|
|
8185
|
+
if (paths.size === 0 && header2 !== null) {
|
|
8186
|
+
for (const path27 of headerPaths(header2)) paths.add(path27);
|
|
8187
8187
|
}
|
|
8188
8188
|
return [...paths];
|
|
8189
8189
|
}
|
|
@@ -11049,13 +11049,22 @@ var ERROR_CODES = {
|
|
|
11049
11049
|
*/
|
|
11050
11050
|
resourceTooLarge: -32e3,
|
|
11051
11051
|
/**
|
|
11052
|
-
* A known resource that this snapshot does not contain.
|
|
11052
|
+
* A known resource that this snapshot does not contain, under a legacy revision.
|
|
11053
11053
|
*
|
|
11054
|
-
* `-32002`
|
|
11055
|
-
*
|
|
11056
|
-
*
|
|
11054
|
+
* `-32002` was the resource-not-found code through protocol revision `2025-11-25`. The
|
|
11055
|
+
* `2026-07-28` revision assigns missing resources to `-32602` and says clients SHOULD still
|
|
11056
|
+
* accept `-32002` from older servers. The dispatcher therefore chooses by era: `-32602` for
|
|
11057
|
+
* modern requests and this code for legacy ones. Answering `-32602` for `snapshot://prompt`
|
|
11058
|
+
* in a snapshot generated with `--skip-artifacts prompt` would tell a legacy client its URI
|
|
11059
|
+
* was wrong when the artifact had simply been left out.
|
|
11057
11060
|
*/
|
|
11058
11061
|
resourceNotFound: -32002,
|
|
11062
|
+
/**
|
|
11063
|
+
* Streamable HTTP headers that are missing, malformed or disagree with the request body.
|
|
11064
|
+
*
|
|
11065
|
+
* Protocol revision `2026-07-28` reserves `-32020` for this. The HTTP status is `400`.
|
|
11066
|
+
*/
|
|
11067
|
+
headerMismatch: -32020,
|
|
11059
11068
|
/**
|
|
11060
11069
|
* The server is at its concurrency limit.
|
|
11061
11070
|
*
|
|
@@ -14675,7 +14684,7 @@ var readTool = {
|
|
|
14675
14684
|
}
|
|
14676
14685
|
const returnedLines = body.length === 0 ? truncatedByBytes && !clippedInsideLine && window.fromLine <= window.totalLines ? 1 : 0 : body.split("\n").length;
|
|
14677
14686
|
const moreRemain = !clippedInsideLine && !window.pastEnd && (window.truncated || truncatedByBytes);
|
|
14678
|
-
const
|
|
14687
|
+
const header2 = [
|
|
14679
14688
|
`${entry.relativePath} \u2014 ${entry.part}, ${entry.layer}/${entry.kind}, ${entry.language}`,
|
|
14680
14689
|
// The line range is the answer to "where am I in this file", and it is computed from the
|
|
14681
14690
|
// body the window was taken from, so it is just as meaningful for a preview as for a full
|
|
@@ -14690,7 +14699,7 @@ var readTool = {
|
|
|
14690
14699
|
] : [],
|
|
14691
14700
|
window.totalLines === 0 ? "This file is empty." : window.pastEnd ? `This file has ${window.totalLines} line(s); fromLine=${window.fromLine} is past its end.` : clippedInsideLine ? `Line ${window.fromLine} of ${window.totalLines}: this line is longer than one result can carry, so this page ends mid-line.` : `Lines ${window.fromLine}\u2013${window.fromLine + returnedLines - 1} of ${window.totalLines}.`
|
|
14692
14701
|
];
|
|
14693
|
-
const lines = [...
|
|
14702
|
+
const lines = [...header2, "", body];
|
|
14694
14703
|
if (moreRemain) {
|
|
14695
14704
|
const next = window.fromLine + returnedLines;
|
|
14696
14705
|
lines.push("", `Truncated. Call snapshot_read with fromLine=${next} for the rest.`);
|
|
@@ -16660,15 +16669,20 @@ function createMcpServer(options) {
|
|
|
16660
16669
|
websiteUrl: options.info.websiteUrl
|
|
16661
16670
|
});
|
|
16662
16671
|
const ok = (id, result, modern) => successResponse(id, result, modern, options.info);
|
|
16672
|
+
const cacheable = (result) => ({
|
|
16673
|
+
...result,
|
|
16674
|
+
cacheScope: "private",
|
|
16675
|
+
ttlMs: 0
|
|
16676
|
+
});
|
|
16663
16677
|
const cancellations = /* @__PURE__ */ new Map();
|
|
16664
16678
|
function handleNotification(method, params) {
|
|
16665
16679
|
if (method === METHOD3.initialized && session.era === "legacy") session.ready = true;
|
|
16666
16680
|
if (method === "notifications/cancelled") {
|
|
16667
|
-
const
|
|
16668
|
-
if (typeof
|
|
16669
|
-
const cancel = cancellations.get(
|
|
16681
|
+
const requestId2 = params?.requestId;
|
|
16682
|
+
if (typeof requestId2 === "string" || typeof requestId2 === "number") {
|
|
16683
|
+
const cancel = cancellations.get(requestId2);
|
|
16670
16684
|
if (cancel !== void 0) {
|
|
16671
|
-
cancellations.delete(
|
|
16685
|
+
cancellations.delete(requestId2);
|
|
16672
16686
|
cancel();
|
|
16673
16687
|
}
|
|
16674
16688
|
}
|
|
@@ -16696,15 +16710,11 @@ function createMcpServer(options) {
|
|
|
16696
16710
|
function discover(id) {
|
|
16697
16711
|
return ok(
|
|
16698
16712
|
id,
|
|
16699
|
-
{
|
|
16713
|
+
cacheable({
|
|
16700
16714
|
supportedVersions: [...SUPPORTED_PROTOCOL_VERSIONS],
|
|
16701
16715
|
capabilities: capabilities(),
|
|
16702
|
-
instructions: options.instructions
|
|
16703
|
-
|
|
16704
|
-
// may be cached only by the client that asked, and not at all after this response.
|
|
16705
|
-
cacheScope: "private",
|
|
16706
|
-
ttlMs: 0
|
|
16707
|
-
},
|
|
16716
|
+
instructions: options.instructions
|
|
16717
|
+
}),
|
|
16708
16718
|
true
|
|
16709
16719
|
);
|
|
16710
16720
|
}
|
|
@@ -16718,7 +16728,7 @@ function createMcpServer(options) {
|
|
|
16718
16728
|
annotations: tool.annotations,
|
|
16719
16729
|
_meta: { "codebase-snapshot/cost": tool.cost }
|
|
16720
16730
|
}));
|
|
16721
|
-
return ok(id, { tools }, modern);
|
|
16731
|
+
return ok(id, cacheable({ tools }), modern);
|
|
16722
16732
|
}
|
|
16723
16733
|
async function callTool(id, modern, params) {
|
|
16724
16734
|
const name = params?.name;
|
|
@@ -16794,7 +16804,7 @@ function createMcpServer(options) {
|
|
|
16794
16804
|
}
|
|
16795
16805
|
async function listResources(id, modern) {
|
|
16796
16806
|
const resources = await options.resources.list(options.context);
|
|
16797
|
-
return ok(id, { resources }, modern);
|
|
16807
|
+
return ok(id, cacheable({ resources }), modern);
|
|
16798
16808
|
}
|
|
16799
16809
|
async function readResource(id, modern, params) {
|
|
16800
16810
|
const uri = params?.uri;
|
|
@@ -16806,7 +16816,12 @@ function createMcpServer(options) {
|
|
|
16806
16816
|
contents = await options.resources.read(uri, options.context);
|
|
16807
16817
|
} catch (error) {
|
|
16808
16818
|
if (error instanceof ResourceNotFoundError) {
|
|
16809
|
-
return errorResponse(
|
|
16819
|
+
return errorResponse(
|
|
16820
|
+
id,
|
|
16821
|
+
modern ? ERROR_CODES.invalidParams : ERROR_CODES.resourceNotFound,
|
|
16822
|
+
error.message,
|
|
16823
|
+
{ uri: error.uri }
|
|
16824
|
+
);
|
|
16810
16825
|
}
|
|
16811
16826
|
if (error instanceof ResourceTooLargeError) {
|
|
16812
16827
|
options.context.log(
|
|
@@ -16823,7 +16838,7 @@ function createMcpServer(options) {
|
|
|
16823
16838
|
if (!contents) {
|
|
16824
16839
|
return errorResponse(id, ERROR_CODES.invalidParams, `Unknown resource "${uri}".`);
|
|
16825
16840
|
}
|
|
16826
|
-
return ok(id, { contents: [contents] }, modern);
|
|
16841
|
+
return ok(id, cacheable({ contents: [contents] }), modern);
|
|
16827
16842
|
}
|
|
16828
16843
|
function listPrompts(id, modern) {
|
|
16829
16844
|
const prompts = options.prompts.map((prompt) => ({
|
|
@@ -16832,7 +16847,7 @@ function createMcpServer(options) {
|
|
|
16832
16847
|
description: prompt.description,
|
|
16833
16848
|
arguments: prompt.arguments
|
|
16834
16849
|
}));
|
|
16835
|
-
return ok(id, { prompts }, modern);
|
|
16850
|
+
return ok(id, cacheable({ prompts }), modern);
|
|
16836
16851
|
}
|
|
16837
16852
|
function getPrompt(id, modern, params) {
|
|
16838
16853
|
const name = params?.name;
|
|
@@ -16901,7 +16916,7 @@ function createMcpServer(options) {
|
|
|
16901
16916
|
case METHOD3.resourcesRead:
|
|
16902
16917
|
return readResource(id, modern, params);
|
|
16903
16918
|
case METHOD3.resourceTemplatesList:
|
|
16904
|
-
return ok(id, { resourceTemplates: options.resources.templates() }, modern);
|
|
16919
|
+
return ok(id, cacheable({ resourceTemplates: options.resources.templates() }), modern);
|
|
16905
16920
|
case METHOD3.promptsList:
|
|
16906
16921
|
return listPrompts(id, modern);
|
|
16907
16922
|
case METHOD3.promptsGet:
|
|
@@ -17207,18 +17222,218 @@ async function runStdioServer(options) {
|
|
|
17207
17222
|
}
|
|
17208
17223
|
|
|
17209
17224
|
// src/auth.ts
|
|
17210
|
-
function parseBearerToken(
|
|
17211
|
-
if (typeof
|
|
17225
|
+
function parseBearerToken(header2) {
|
|
17226
|
+
if (typeof header2 !== "string") return null;
|
|
17212
17227
|
const scheme = "bearer";
|
|
17213
|
-
if (
|
|
17214
|
-
if (
|
|
17228
|
+
if (header2.length <= scheme.length) return null;
|
|
17229
|
+
if (header2.slice(0, scheme.length).toLowerCase() !== scheme) return null;
|
|
17215
17230
|
let index = scheme.length;
|
|
17216
|
-
if (
|
|
17217
|
-
while (index <
|
|
17218
|
-
const token =
|
|
17231
|
+
if (header2[index] !== " " && header2[index] !== " ") return null;
|
|
17232
|
+
while (index < header2.length && (header2[index] === " " || header2[index] === " ")) index += 1;
|
|
17233
|
+
const token = header2.slice(index).trim();
|
|
17219
17234
|
return token.length > 0 ? token : null;
|
|
17220
17235
|
}
|
|
17221
17236
|
|
|
17237
|
+
// src/mcp/http-headers.ts
|
|
17238
|
+
var PROTOCOL_HEADER = "mcp-protocol-version";
|
|
17239
|
+
var METHOD_HEADER = "mcp-method";
|
|
17240
|
+
var NAME_HEADER = "mcp-name";
|
|
17241
|
+
var BASE64_PREFIX = "=?base64?";
|
|
17242
|
+
var BASE64_SUFFIX = "?=";
|
|
17243
|
+
var NAMED_METHODS = /* @__PURE__ */ new Set(["tools/call", "resources/read", "prompts/get"]);
|
|
17244
|
+
function requestId(message) {
|
|
17245
|
+
const raw = message.id;
|
|
17246
|
+
return typeof raw === "string" || typeof raw === "number" ? raw : null;
|
|
17247
|
+
}
|
|
17248
|
+
function header(request, name) {
|
|
17249
|
+
const value = request.headers[name];
|
|
17250
|
+
return typeof value === "string" ? value : null;
|
|
17251
|
+
}
|
|
17252
|
+
function isSafeHeaderValue(value) {
|
|
17253
|
+
return /^[\x20-\x7e\t]+$/.test(value);
|
|
17254
|
+
}
|
|
17255
|
+
function decodeMcpHeaderValue(value) {
|
|
17256
|
+
if (!value.startsWith(BASE64_PREFIX)) {
|
|
17257
|
+
return isSafeHeaderValue(value) ? value : null;
|
|
17258
|
+
}
|
|
17259
|
+
if (!value.endsWith(BASE64_SUFFIX)) return null;
|
|
17260
|
+
const encoded = value.slice(BASE64_PREFIX.length, -BASE64_SUFFIX.length);
|
|
17261
|
+
if (encoded.length === 0 || encoded.length % 4 !== 0) return null;
|
|
17262
|
+
if (!/^[A-Za-z0-9+/]*={0,2}$/.test(encoded)) return null;
|
|
17263
|
+
const decoded = Buffer.from(encoded, "base64").toString("utf8");
|
|
17264
|
+
if (Buffer.from(decoded, "utf8").toString("base64") !== encoded) return null;
|
|
17265
|
+
return decoded;
|
|
17266
|
+
}
|
|
17267
|
+
function isModernHttpMessage(request, message) {
|
|
17268
|
+
if (Array.isArray(message)) {
|
|
17269
|
+
return message.some((item) => isModernHttpMessage(request, item));
|
|
17270
|
+
}
|
|
17271
|
+
if (!isJsonObject(message)) return false;
|
|
17272
|
+
if (message.method === "server/discover") return true;
|
|
17273
|
+
const params = isJsonObject(message.params) ? message.params : void 0;
|
|
17274
|
+
const meta = isJsonObject(params?._meta) ? params._meta : void 0;
|
|
17275
|
+
if (meta !== void 0 && (META_KEYS.protocolVersion in meta || META_KEYS.clientCapabilities in meta)) {
|
|
17276
|
+
return true;
|
|
17277
|
+
}
|
|
17278
|
+
return header(request, METHOD_HEADER) !== null || header(request, NAME_HEADER) !== null;
|
|
17279
|
+
}
|
|
17280
|
+
function refuse(id, code, message, data) {
|
|
17281
|
+
return errorResponse(id, code, message, data);
|
|
17282
|
+
}
|
|
17283
|
+
function validateModernHttpHeaders(request, message) {
|
|
17284
|
+
if (!isModernHttpMessage(request, message)) return { modern: false };
|
|
17285
|
+
if (Array.isArray(message)) {
|
|
17286
|
+
return {
|
|
17287
|
+
modern: true,
|
|
17288
|
+
error: refuse(null, ERROR_CODES.invalidRequest, "Batch requests are not supported.")
|
|
17289
|
+
};
|
|
17290
|
+
}
|
|
17291
|
+
if (!isJsonObject(message)) {
|
|
17292
|
+
return {
|
|
17293
|
+
modern: true,
|
|
17294
|
+
error: refuse(null, ERROR_CODES.invalidRequest, "Message is not a JSON object.")
|
|
17295
|
+
};
|
|
17296
|
+
}
|
|
17297
|
+
const id = requestId(message);
|
|
17298
|
+
const method = message.method;
|
|
17299
|
+
const params = isJsonObject(message.params) ? message.params : void 0;
|
|
17300
|
+
if (typeof method !== "string" || method.length === 0) {
|
|
17301
|
+
return {
|
|
17302
|
+
modern: true,
|
|
17303
|
+
error: refuse(id, ERROR_CODES.invalidRequest, 'Message must carry a "method" string.')
|
|
17304
|
+
};
|
|
17305
|
+
}
|
|
17306
|
+
const versionHeader = header(request, PROTOCOL_HEADER);
|
|
17307
|
+
if (versionHeader === null) {
|
|
17308
|
+
return {
|
|
17309
|
+
modern: true,
|
|
17310
|
+
error: refuse(
|
|
17311
|
+
id,
|
|
17312
|
+
ERROR_CODES.headerMismatch,
|
|
17313
|
+
`Header mismatch: ${PROTOCOL_HEADER} is required for a modern request.`
|
|
17314
|
+
)
|
|
17315
|
+
};
|
|
17316
|
+
}
|
|
17317
|
+
if (!isSafeHeaderValue(versionHeader)) {
|
|
17318
|
+
return {
|
|
17319
|
+
modern: true,
|
|
17320
|
+
error: refuse(
|
|
17321
|
+
id,
|
|
17322
|
+
ERROR_CODES.headerMismatch,
|
|
17323
|
+
`Header mismatch: ${PROTOCOL_HEADER} contains invalid characters.`
|
|
17324
|
+
)
|
|
17325
|
+
};
|
|
17326
|
+
}
|
|
17327
|
+
const meta = inspectModernMeta(params);
|
|
17328
|
+
if (meta.kind !== "present") {
|
|
17329
|
+
const missing = meta.kind === "incomplete" ? meta.missing : META_KEYS.protocolVersion;
|
|
17330
|
+
return {
|
|
17331
|
+
modern: true,
|
|
17332
|
+
error: refuse(
|
|
17333
|
+
id,
|
|
17334
|
+
ERROR_CODES.invalidParams,
|
|
17335
|
+
`The request _meta is missing the required key "${missing}".`
|
|
17336
|
+
)
|
|
17337
|
+
};
|
|
17338
|
+
}
|
|
17339
|
+
if (versionHeader !== meta.meta.version) {
|
|
17340
|
+
return {
|
|
17341
|
+
modern: true,
|
|
17342
|
+
error: refuse(
|
|
17343
|
+
id,
|
|
17344
|
+
ERROR_CODES.headerMismatch,
|
|
17345
|
+
`Header mismatch: ${PROTOCOL_HEADER} "${versionHeader}" does not match body value "${meta.meta.version}".`
|
|
17346
|
+
)
|
|
17347
|
+
};
|
|
17348
|
+
}
|
|
17349
|
+
const methodHeader = header(request, METHOD_HEADER);
|
|
17350
|
+
if (methodHeader === null) {
|
|
17351
|
+
return {
|
|
17352
|
+
modern: true,
|
|
17353
|
+
error: refuse(
|
|
17354
|
+
id,
|
|
17355
|
+
ERROR_CODES.headerMismatch,
|
|
17356
|
+
`Header mismatch: ${METHOD_HEADER} is required for a modern request.`
|
|
17357
|
+
)
|
|
17358
|
+
};
|
|
17359
|
+
}
|
|
17360
|
+
if (!isSafeHeaderValue(methodHeader) || methodHeader !== method) {
|
|
17361
|
+
return {
|
|
17362
|
+
modern: true,
|
|
17363
|
+
error: refuse(
|
|
17364
|
+
id,
|
|
17365
|
+
ERROR_CODES.headerMismatch,
|
|
17366
|
+
`Header mismatch: ${METHOD_HEADER} value "${methodHeader}" does not match body value "${method}".`
|
|
17367
|
+
)
|
|
17368
|
+
};
|
|
17369
|
+
}
|
|
17370
|
+
if (NAMED_METHODS.has(method)) {
|
|
17371
|
+
const nameHeader = header(request, NAME_HEADER);
|
|
17372
|
+
if (nameHeader === null) {
|
|
17373
|
+
return {
|
|
17374
|
+
modern: true,
|
|
17375
|
+
error: refuse(
|
|
17376
|
+
id,
|
|
17377
|
+
ERROR_CODES.headerMismatch,
|
|
17378
|
+
`Header mismatch: ${NAME_HEADER} is required for ${method}.`
|
|
17379
|
+
)
|
|
17380
|
+
};
|
|
17381
|
+
}
|
|
17382
|
+
const decoded = decodeMcpHeaderValue(nameHeader);
|
|
17383
|
+
if (decoded === null) {
|
|
17384
|
+
return {
|
|
17385
|
+
modern: true,
|
|
17386
|
+
error: refuse(
|
|
17387
|
+
id,
|
|
17388
|
+
ERROR_CODES.headerMismatch,
|
|
17389
|
+
`Header mismatch: ${NAME_HEADER} contains an invalid value.`
|
|
17390
|
+
)
|
|
17391
|
+
};
|
|
17392
|
+
}
|
|
17393
|
+
const bodyValue = method === "resources/read" ? params?.uri : params?.name;
|
|
17394
|
+
if (typeof bodyValue !== "string") {
|
|
17395
|
+
return {
|
|
17396
|
+
modern: true,
|
|
17397
|
+
error: refuse(
|
|
17398
|
+
id,
|
|
17399
|
+
ERROR_CODES.invalidParams,
|
|
17400
|
+
`${method} requires a "${method === "resources/read" ? "uri" : "name"}" string.`
|
|
17401
|
+
)
|
|
17402
|
+
};
|
|
17403
|
+
}
|
|
17404
|
+
if (decoded !== bodyValue) {
|
|
17405
|
+
return {
|
|
17406
|
+
modern: true,
|
|
17407
|
+
error: refuse(
|
|
17408
|
+
id,
|
|
17409
|
+
ERROR_CODES.headerMismatch,
|
|
17410
|
+
`Header mismatch: ${NAME_HEADER} value "${decoded}" does not match body value "${bodyValue}".`
|
|
17411
|
+
)
|
|
17412
|
+
};
|
|
17413
|
+
}
|
|
17414
|
+
}
|
|
17415
|
+
return { modern: true };
|
|
17416
|
+
}
|
|
17417
|
+
function modernHttpStatus(response) {
|
|
17418
|
+
if (!("error" in response)) return 200;
|
|
17419
|
+
switch (response.error.code) {
|
|
17420
|
+
case ERROR_CODES.headerMismatch:
|
|
17421
|
+
case ERROR_CODES.unsupportedProtocolVersion:
|
|
17422
|
+
case ERROR_CODES.invalidParams:
|
|
17423
|
+
case ERROR_CODES.invalidRequest:
|
|
17424
|
+
case ERROR_CODES.parseError:
|
|
17425
|
+
return 400;
|
|
17426
|
+
case ERROR_CODES.methodNotFound:
|
|
17427
|
+
return 404;
|
|
17428
|
+
case ERROR_CODES.internalError:
|
|
17429
|
+
return 500;
|
|
17430
|
+
case ERROR_CODES.overloaded:
|
|
17431
|
+
return 503;
|
|
17432
|
+
default:
|
|
17433
|
+
return 200;
|
|
17434
|
+
}
|
|
17435
|
+
}
|
|
17436
|
+
|
|
17222
17437
|
// src/mcp/http.ts
|
|
17223
17438
|
var DEFAULT_PORT = 4789;
|
|
17224
17439
|
var DEFAULT_MAX_BODY = 2 * 1024 * 1024;
|
|
@@ -17249,8 +17464,8 @@ function timingSafeEqual2(left, right) {
|
|
|
17249
17464
|
return crypto.timingSafeEqual(a, b);
|
|
17250
17465
|
}
|
|
17251
17466
|
function sessionHeader(request) {
|
|
17252
|
-
const
|
|
17253
|
-
return typeof
|
|
17467
|
+
const header2 = request.headers["mcp-session-id"];
|
|
17468
|
+
return typeof header2 === "string" ? header2.trim() : "";
|
|
17254
17469
|
}
|
|
17255
17470
|
function isObject(value) {
|
|
17256
17471
|
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
@@ -17261,12 +17476,6 @@ function isToolCall(message) {
|
|
|
17261
17476
|
function methodOf(message) {
|
|
17262
17477
|
return isObject(message) && typeof message.method === "string" ? message.method : null;
|
|
17263
17478
|
}
|
|
17264
|
-
function hasModernMeta(message) {
|
|
17265
|
-
const messages = Array.isArray(message) ? message : [message];
|
|
17266
|
-
return messages.some(
|
|
17267
|
-
(item) => isJsonObject(item) && isJsonObject(item.params) && inspectModernMeta(item.params).kind === "present"
|
|
17268
|
-
);
|
|
17269
|
-
}
|
|
17270
17479
|
function createMcpHttpHandler(options) {
|
|
17271
17480
|
const log = options.log ?? ((line) => process.stderr.write(line + "\n"));
|
|
17272
17481
|
const token = options.token;
|
|
@@ -17392,7 +17601,7 @@ function createMcpHttpHandler(options) {
|
|
|
17392
17601
|
return {
|
|
17393
17602
|
"access-control-allow-origin": allowed,
|
|
17394
17603
|
vary: "origin",
|
|
17395
|
-
"access-control-allow-headers": "authorization, content-type, mcp-protocol-version, mcp-session-id",
|
|
17604
|
+
"access-control-allow-headers": "authorization, content-type, mcp-protocol-version, mcp-session-id, mcp-method, mcp-name",
|
|
17396
17605
|
// Without this a browser can send the header but cannot read it: mcp-session-id is not a
|
|
17397
17606
|
// CORS-safelisted response header, so every cross-origin client silently lost its session.
|
|
17398
17607
|
"access-control-expose-headers": "mcp-session-id",
|
|
@@ -17600,6 +17809,12 @@ function createMcpHttpHandler(options) {
|
|
|
17600
17809
|
send(response, 400, { error: "JSON-RPC batch is limited to 100 messages" }, cors);
|
|
17601
17810
|
return;
|
|
17602
17811
|
}
|
|
17812
|
+
const headers = validateModernHttpHeaders(request, message);
|
|
17813
|
+
if (headers.error !== void 0) {
|
|
17814
|
+
send(response, 400, headers.error, cors);
|
|
17815
|
+
return;
|
|
17816
|
+
}
|
|
17817
|
+
const modern = headers.modern;
|
|
17603
17818
|
if (batch.length > 1 && rateLimited(request, batch.length - 1)) {
|
|
17604
17819
|
tooMany(response, cors);
|
|
17605
17820
|
return;
|
|
@@ -17618,6 +17833,8 @@ function createMcpHttpHandler(options) {
|
|
|
17618
17833
|
rememberAnonymous(anonymousKey(request), server);
|
|
17619
17834
|
responseSession = newSessionId();
|
|
17620
17835
|
rememberSession(responseSession, server, Date.now());
|
|
17836
|
+
} else if (modern) {
|
|
17837
|
+
server = createMcp();
|
|
17621
17838
|
} else if (supplied !== "") {
|
|
17622
17839
|
const now = Date.now();
|
|
17623
17840
|
pruneSessions(now);
|
|
@@ -17629,8 +17846,6 @@ function createMcpHttpHandler(options) {
|
|
|
17629
17846
|
existing.lastSeen = now;
|
|
17630
17847
|
server = existing.server;
|
|
17631
17848
|
responseSession = supplied;
|
|
17632
|
-
} else if (hasModernMeta(message)) {
|
|
17633
|
-
server = createMcp();
|
|
17634
17849
|
} else {
|
|
17635
17850
|
server = fallbackServer(request);
|
|
17636
17851
|
}
|
|
@@ -17654,7 +17869,8 @@ function createMcpHttpHandler(options) {
|
|
|
17654
17869
|
send(response, 202, "", sessionHeaders);
|
|
17655
17870
|
return;
|
|
17656
17871
|
}
|
|
17657
|
-
|
|
17872
|
+
const status = modern ? modernHttpStatus(answers[0]) : 200;
|
|
17873
|
+
send(response, status, Array.isArray(message) ? answers : answers[0], sessionHeaders);
|
|
17658
17874
|
})().catch((error) => {
|
|
17659
17875
|
log("mcp/http: " + error.message);
|
|
17660
17876
|
send(response, 500, { jsonrpc: "2.0", id: null, error: { code: -32603, message: "Internal error" } }, cors);
|
|
@@ -17786,7 +18002,7 @@ async function startHttpTransport(options) {
|
|
|
17786
18002
|
if (allowed !== null) {
|
|
17787
18003
|
cors["access-control-allow-origin"] = allowed;
|
|
17788
18004
|
cors.vary = "origin";
|
|
17789
|
-
cors["access-control-allow-headers"] = "authorization, content-type, mcp-protocol-version, mcp-session-id";
|
|
18005
|
+
cors["access-control-allow-headers"] = "authorization, content-type, mcp-protocol-version, mcp-session-id, mcp-method, mcp-name";
|
|
17790
18006
|
cors["access-control-expose-headers"] = "mcp-session-id";
|
|
17791
18007
|
cors["access-control-allow-methods"] = "POST, GET, OPTIONS, DELETE";
|
|
17792
18008
|
cors["access-control-max-age"] = "600";
|
|
@@ -19837,13 +20053,13 @@ function auditSnapshot(options) {
|
|
|
19837
20053
|
const text2 = partTexts.get(part.filename);
|
|
19838
20054
|
const sections = partSections.get(part.filename);
|
|
19839
20055
|
if (text2 === void 0 || sections === void 0) continue;
|
|
19840
|
-
const
|
|
19841
|
-
if (
|
|
19842
|
-
if (Number(
|
|
19843
|
-
structureProblems.push(part.filename + " says part " +
|
|
20056
|
+
const header2 = /^# Codebase Snapshot Part (\d+) of (\d+)$/m.exec(text2);
|
|
20057
|
+
if (header2 !== null) {
|
|
20058
|
+
if (Number(header2[1]) !== part.index) {
|
|
20059
|
+
structureProblems.push(part.filename + " says part " + header2[1] + ", the manifest says " + part.index);
|
|
19844
20060
|
}
|
|
19845
|
-
if (Number(
|
|
19846
|
-
structureProblems.push(part.filename + " says " +
|
|
20061
|
+
if (Number(header2[2]) !== manifest.parts.length) {
|
|
20062
|
+
structureProblems.push(part.filename + " says " + header2[2] + " parts, the manifest holds " + manifest.parts.length);
|
|
19847
20063
|
}
|
|
19848
20064
|
}
|
|
19849
20065
|
const filesLine = /^- Files: (\d+)$/m.exec(text2);
|