@unbrained/pm-cli 2026.8.24 → 2026.8.26
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/.claude-plugin/marketplace.json +2 -2
- package/CHANGELOG.md +46 -0
- package/dist/cli-bundle/bundle-manifest.json +149 -149
- package/dist/cli-bundle/chunks/chunk-E2GCFJSU.js +35 -0
- package/dist/cli-bundle/chunks/{chunk-TEQDR5KI.js → chunk-ES25LX3D.js} +62 -62
- package/dist/cli-bundle/chunks/{chunk-I5Q7QSZX.js → chunk-FRDWWB6R.js} +2 -2
- package/dist/cli-bundle/chunks/{chunk-X7WWQDYQ.js → chunk-ICQ3RVIY.js} +2 -2
- package/dist/cli-bundle/chunks/{chunk-YOLLRCLY.js → chunk-IV64RJVE.js} +2 -2
- package/dist/cli-bundle/chunks/{chunk-L22NP5MA.js → chunk-KBFP3E4E.js} +86 -86
- package/dist/cli-bundle/chunks/{chunk-AGUL3HUT.js → chunk-MVYLQ67M.js} +2 -2
- package/dist/cli-bundle/chunks/{chunk-DJNJBWAQ.js → chunk-S4U76VZF.js} +2 -2
- package/dist/cli-bundle/chunks/{chunk-IW72J5FW.js → chunk-UKBCRPA2.js} +2 -2
- package/dist/cli-bundle/chunks/chunk-WRHJ3MB6.js +8 -0
- package/dist/cli-bundle/chunks/{chunk-ZAVCS2GH.js → chunk-ZNRLJ54C.js} +60 -60
- package/dist/cli-bundle/chunks/{register-list-query-37A4P6O4.js → register-list-query-EMCPMICY.js} +2 -2
- package/dist/cli-bundle/chunks/{register-mutation-TBJDHYIT.js → register-mutation-OJ67ABCB.js} +3 -3
- package/dist/cli-bundle/chunks/{register-operations-WRZNEBUM.js → register-operations-H2GLP7LT.js} +2 -2
- package/dist/cli-bundle/chunks/{register-setup-H4PDLACD.js → register-setup-GLZAHLVI.js} +2 -2
- package/dist/cli-bundle/focused-chunks/{chunk-IMWTYKII.js → chunk-4K2II4TV.js} +2 -2
- package/dist/cli-bundle/focused-chunks/chunk-4XNH2HM7.js +2 -0
- package/dist/cli-bundle/focused-chunks/{chunk-GJR6YEMR.js → chunk-57XY346D.js} +2 -2
- package/dist/cli-bundle/focused-chunks/{chunk-4WP7LNE3.js → chunk-66VGB23P.js} +2 -2
- package/dist/cli-bundle/focused-chunks/chunk-72T6JGAE.js +50 -0
- package/dist/cli-bundle/focused-chunks/{chunk-BFA6KKCI.js → chunk-7I23XGWO.js} +2 -2
- package/dist/cli-bundle/focused-chunks/{chunk-6Z24ZMU3.js → chunk-7YCDTCBC.js} +2 -2
- package/dist/cli-bundle/focused-chunks/{chunk-O4ZXNJMT.js → chunk-A644DUFQ.js} +2 -2
- package/dist/cli-bundle/focused-chunks/chunk-FXDLT6FL.js +16 -0
- package/dist/cli-bundle/focused-chunks/{chunk-RASVBJX5.js → chunk-IBHXMFE7.js} +2 -2
- package/dist/cli-bundle/focused-chunks/chunk-LMKG3DFE.js +5 -0
- package/dist/cli-bundle/focused-chunks/{chunk-6MUTDXMP.js → chunk-LV5N3LK5.js} +2 -2
- package/dist/cli-bundle/focused-chunks/{chunk-CWCN5RLO.js → chunk-MMXUPDDJ.js} +2 -2
- package/dist/cli-bundle/focused-chunks/{chunk-WZLSSGEU.js → chunk-OHIHZ7HS.js} +2 -2
- package/dist/cli-bundle/focused-chunks/chunk-P2E6LDAE.js +19 -0
- package/dist/cli-bundle/focused-chunks/{chunk-IZNRRCU3.js → chunk-TMJDFHVD.js} +2 -2
- package/dist/cli-bundle/focused-chunks/{chunk-QBP5FSBX.js → chunk-UYBA57GY.js} +4 -4
- package/dist/cli-bundle/focused-chunks/{chunk-MNPECIRL.js → chunk-YO3ZF3FI.js} +2 -2
- package/dist/cli-bundle/main.js +2 -2
- package/dist/cli-bundle/sdk-authoring.js +1 -1
- package/dist/cli-bundle/sdk-contracts.js +2 -2
- package/dist/cli-bundle/sdk-core.js +41 -41
- package/dist/cli-bundle/sdk-governance.js +1 -1
- package/dist/cli-bundle/sdk-graph.js +1 -1
- package/dist/cli-bundle/sdk-merge.js +32 -33
- package/dist/cli-bundle/sdk-query.js +1 -1
- package/dist/cli-bundle/sdk-runtime.js +1 -1
- package/dist/cli-bundle/sdk-testing.js +1 -1
- package/dist/cli-bundle/sdk.js +7 -6
- package/dist/core/diagnostics/remediation.js +17 -2
- package/dist/core/extensions/manifest-schema.d.ts +20 -0
- package/dist/core/extensions/manifest-schema.js +28 -10
- package/dist/mcp/http-server.d.ts +60 -0
- package/dist/mcp/http-server.js +451 -0
- package/dist/mcp/legacy-adapter.d.ts +50 -0
- package/dist/mcp/legacy-adapter.js +64 -0
- package/dist/mcp/server.d.ts +42 -9
- package/dist/mcp/server.js +457 -58
- package/dist/sdk/agent/refusal-closure-census.d.ts +6 -2
- package/dist/sdk/agent/refusal-closure-census.js +16 -8
- package/dist/sdk/cli-contracts/runtime-contracts.d.ts +10 -0
- package/dist/sdk/cli-contracts/runtime-contracts.js +20 -3
- package/dist/sdk/compose.d.ts +4 -1
- package/dist/sdk/compose.js +23 -36
- package/dist/sdk/extension/author-manifest.d.ts +22 -0
- package/dist/sdk/extension/author-manifest.js +93 -0
- package/dist/sdk/extension.js +6 -3
- package/dist/sdk/generated/generated-error-code-catalog-part-1.js +67 -28
- package/dist/sdk/generated/generated-error-code-catalog-part-2.js +30 -3
- package/dist/sdk/governance/health.js +103 -11
- package/dist/sdk/governance/validate.js +15 -9
- package/dist/sdk/history-repair.d.ts +24 -0
- package/dist/sdk/history-repair.js +145 -4
- package/dist/sdk/index.d.ts +6 -0
- package/dist/sdk/index.js +8 -2
- package/dist/sdk/mcp/authorization.d.ts +134 -0
- package/dist/sdk/mcp/authorization.js +405 -0
- package/dist/sdk/mcp/interactions.d.ts +118 -0
- package/dist/sdk/mcp/interactions.js +337 -0
- package/dist/sdk/mcp/protocol.d.ts +142 -0
- package/dist/sdk/mcp/protocol.js +174 -0
- package/dist/sdk/mcp/subscriptions.d.ts +65 -0
- package/dist/sdk/mcp/subscriptions.js +212 -0
- package/dist/sdk/mcp/tasks.d.ts +107 -0
- package/dist/sdk/mcp/tasks.js +431 -0
- package/dist/sdk/mcp/transport.d.ts +30 -0
- package/dist/sdk/mcp/transport.js +261 -0
- package/dist/sdk/merge/driver.js +25 -4
- package/dist/sdk/merge/receipt-file-boundary.d.ts +21 -0
- package/dist/sdk/merge/receipt-file-boundary.js +47 -0
- package/dist/sdk/merge/receipts.d.ts +40 -1
- package/dist/sdk/merge/receipts.js +343 -36
- package/dist/sdk/merge/reconcile.js +117 -47
- package/dist/sdk/read-output/continuation.d.ts +15 -2
- package/dist/sdk/read-output/continuation.js +86 -6
- package/dist/sdk/read-output-contracts.d.ts +4 -1
- package/dist/sdk/read-output-contracts.js +9 -6
- package/dist/sdk/runtime-primitives.d.ts +1 -1
- package/dist/sdk/runtime-primitives.js +3 -3
- package/dist/sdk/test/execution.d.ts +6 -0
- package/dist/sdk/test/execution.js +32 -3
- package/docs/AGENT_PROVENANCE_ADR.md +6 -4
- package/docs/AGENT_RUNTIME_PRIMITIVES.md +6 -5
- package/docs/CLAUDE_CODE_PLUGIN.md +12 -5
- package/docs/DIAGNOSTIC_OUTPUT_CONTRACTS.md +8 -0
- package/docs/EXTENSIONS.md +3 -4
- package/docs/MCP_2026_07_28.md +138 -0
- package/docs/MCP_2026_07_28_CONFORMANCE.md +30 -0
- package/docs/MCP_REMOTE_TRANSPORT_SECURITY.md +180 -0
- package/docs/MERGE_SAFETY.md +34 -5
- package/docs/OUTPUT_PROJECTION_CONTRACTS.md +13 -1
- package/docs/README.md +4 -0
- package/docs/READ_OUTPUT_CONTRACTS.md +25 -2
- package/docs/RELEASING.md +3 -2
- package/docs/SDK.md +22 -1
- package/docs/SDK_AGENT_SESSION_CONTEXT.md +18 -13
- package/docs/SDK_MCP_INTERACTIONS.md +227 -0
- package/docs/TESTING.md +4 -0
- package/docs/generated/REFUSAL_CLOSURE_CENSUS.md +13 -8
- package/marketplace.json +2 -2
- package/package.json +7 -5
- package/packages/pm-beads/package.json +1 -1
- package/packages/pm-calendar/package.json +1 -1
- package/packages/pm-command-kit/package.json +1 -1
- package/packages/pm-digital-twin/package.json +1 -1
- package/packages/pm-governance-audit/package.json +1 -1
- package/packages/pm-guide-shell/package.json +1 -1
- package/packages/pm-kanban/package.json +1 -1
- package/packages/pm-lifecycle-hooks/package.json +1 -1
- package/packages/pm-linked-test-adapters/package.json +1 -1
- package/packages/pm-search-advanced/package.json +1 -1
- package/packages/pm-templates/package.json +1 -1
- package/packages/pm-todos/package.json +1 -1
- package/packages/pm-vcs/package.json +1 -1
- package/plugins/pm-claude/.claude-plugin/plugin.json +1 -1
- package/plugins/pm-codex/.codex-plugin/plugin.json +1 -1
- package/scripts/finalize-build.mjs +1 -0
- package/sdk/public-surface.json +762 -15
- package/dist/cli-bundle/chunks/chunk-MKE6MD2K.js +0 -36
- package/dist/cli-bundle/chunks/chunk-NXBFO63A.js +0 -8
- package/dist/cli-bundle/focused-chunks/chunk-FCTT4CZU.js +0 -5
- package/dist/cli-bundle/focused-chunks/chunk-Q63PSLO4.js +0 -2
- package/dist/cli-bundle/focused-chunks/chunk-QV5UP27O.js +0 -51
- package/dist/cli-bundle/focused-chunks/chunk-RPRLENVA.js +0 -16
- package/dist/cli-bundle/focused-chunks/chunk-TOFVXBWA.js +0 -19
package/dist/mcp/server.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* Runs the MCP server adapter that exposes pm actions and contracts to external agents.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="
|
|
8
|
+
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="2621052e-5b3f-5ab2-b587-036f375ee187")}catch(e){}}();
|
|
9
9
|
import { realpathSync } from "node:fs";
|
|
10
10
|
import { readFile } from "node:fs/promises";
|
|
11
11
|
import path from "node:path";
|
|
@@ -23,6 +23,12 @@ import { attachOutputTokenAccounting } from "../sdk/output-token-accounting.js";
|
|
|
23
23
|
import { createReproducibleProcessRunner, runWithReproducibleProcessEnvironment, } from "../sdk/reproducibility/process.js";
|
|
24
24
|
import { parseAtomicMutationControls, resolveItemMutationDocument, } from "../sdk/structured-mutations.js";
|
|
25
25
|
import { resolveAuthor, resolvePmRoot } from "../sdk/runtime-primitives.js";
|
|
26
|
+
import { PM_MCP_ERROR_CODES, PM_MCP_META_KEYS, PM_MCP_PROTOCOL_VERSION, PmMcpProtocolError, attachMcpServerInfo, buildMcpCompleteResult, buildMcpDiscoverResult, hasMcpClientExtension, isMcpRecord, resolveMcpRequestContext, } from "../sdk/mcp/protocol.js";
|
|
27
|
+
import { PmMcpInputRequiredError, buildMcpInputRequiredResult, parseMcpInputResponses, validateMcpJsonSchema, withMcpCachePolicy, } from "../sdk/mcp/interactions.js";
|
|
28
|
+
import { PM_MCP_TASKS_EXTENSION, createMcpTaskStore, } from "../sdk/mcp/tasks.js";
|
|
29
|
+
import { PmMcpSubscriptionRegistry, } from "../sdk/mcp/subscriptions.js";
|
|
30
|
+
import { extractMcpTraceContext, runWithMcpTraceContext, } from "../sdk/mcp/authorization.js";
|
|
31
|
+
import { LegacyMcpAdapter } from "./legacy-adapter.js";
|
|
26
32
|
function runMcpAction(args) {
|
|
27
33
|
return runAction(markMcpMutationTransportInput(args));
|
|
28
34
|
}
|
|
@@ -37,7 +43,59 @@ if (typeof process.env[PM_PACKAGE_ROOT_ENV] !== "string" ||
|
|
|
37
43
|
// Reflect the real package.json version so agents/telemetry can identify the
|
|
38
44
|
// build serving requests (was hard-coded "1.0.0"; see pm-2nvw).
|
|
39
45
|
const PM_MCP_SERVER_VERSION = resolvePmCliVersion(import.meta.url, ["../.."]) ?? "0.0.0";
|
|
40
|
-
|
|
46
|
+
const PM_MCP_SERVER_INFO = {
|
|
47
|
+
name: "pm-mcp",
|
|
48
|
+
version: PM_MCP_SERVER_VERSION,
|
|
49
|
+
description: "Stateless project-context management through pm SDK contracts.",
|
|
50
|
+
websiteUrl: "https://github.com/unbraind/pm-cli",
|
|
51
|
+
};
|
|
52
|
+
const PM_MCP_SERVER_CAPABILITIES = {
|
|
53
|
+
prompts: { listChanged: true },
|
|
54
|
+
resources: { listChanged: true, subscribe: true },
|
|
55
|
+
tools: { listChanged: true },
|
|
56
|
+
extensions: { [PM_MCP_TASKS_EXTENSION]: {} },
|
|
57
|
+
};
|
|
58
|
+
const PM_MCP_SUBSCRIPTIONS = new Map();
|
|
59
|
+
function createMcpSubscriptionRegistry() {
|
|
60
|
+
return new PmMcpSubscriptionRegistry({
|
|
61
|
+
capabilities: PM_MCP_SERVER_CAPABILITIES,
|
|
62
|
+
serverInfo: PM_MCP_SERVER_INFO,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
function pruneClosedMcpSubscriptionRegistries() {
|
|
66
|
+
for (const [key, subscription] of PM_MCP_SUBSCRIPTIONS) {
|
|
67
|
+
if (subscription.registry.size === 0)
|
|
68
|
+
PM_MCP_SUBSCRIPTIONS.delete(key);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const PM_MCP_CACHE_POLICIES = {
|
|
72
|
+
prompts: { ttlMs: 60_000, cacheScope: "public" },
|
|
73
|
+
resource: { ttlMs: 0, cacheScope: "private" },
|
|
74
|
+
resources: { ttlMs: 60_000, cacheScope: "public" },
|
|
75
|
+
tools: { ttlMs: 30_000, cacheScope: "private" },
|
|
76
|
+
};
|
|
77
|
+
const PM_MCP_DIRECT_TASK_TOOLS = new Set([
|
|
78
|
+
"pm_graph",
|
|
79
|
+
"pm_health",
|
|
80
|
+
"pm_validate",
|
|
81
|
+
]);
|
|
82
|
+
const PM_MCP_RUN_TASK_ACTIONS = new Set([
|
|
83
|
+
"graph",
|
|
84
|
+
"health",
|
|
85
|
+
"import",
|
|
86
|
+
"reindex",
|
|
87
|
+
"test-all",
|
|
88
|
+
"validate",
|
|
89
|
+
]);
|
|
90
|
+
const PM_MCP_INSTRUCTIONS = "You have access to native pm CLI tools for git-based project management. " +
|
|
91
|
+
"Use pm_next to pick the next actionable item, or pm_context or pm_search before creating new work. " +
|
|
92
|
+
"Prefer narrow tools (pm_next, pm_context, pm_list, pm_get, pm_search, pm_events, pm_create, pm_mutate, pm_copy, pm_focus, pm_update, pm_append, pm_claim, pm_release, pm_close, pm_comments, pm_files, pm_docs, pm_notes, pm_learnings, pm_deps, pm_graph, pm_test, pm_validate, pm_health, pm_contracts, pm_schema, pm_profile, pm_config, pm_plan) over pm_run when they cover the operation. " +
|
|
93
|
+
"Use pm_plan for agent harness Plan workflows: it provides Codex/Claude/Cursor-style planning with durable steps, dependencies, decisions, discoveries, validation, and materialization. " +
|
|
94
|
+
"Use pm_schema and pm_config for workspace configuration: pm_schema manages custom item types/statuses and pm_config reads or writes settings keys. " +
|
|
95
|
+
"Use pm_run with an explicit action for active package-owned operations, plus activity, aggregate, history, stats, test-all, and gc. " +
|
|
96
|
+
"Use history-redact for audited history-stream redaction workflows, history-repair to re-anchor a drifted history chain, and history-compact to checkpoint/prune long history streams while preserving replay integrity. " +
|
|
97
|
+
"Agent harness and model provenance are detected automatically; pass author only for an intentional identity override. " +
|
|
98
|
+
"Do not pass path during real repository tracking — only pass path for sandbox or test runs.";
|
|
41
99
|
// Tool definitions (TOOLS) live in ./tool-definitions.ts so the `pm contracts`
|
|
42
100
|
// golden-file snapshot can import the surface without loading the server
|
|
43
101
|
// runtime (pm-4os2). This file owns dispatch, normalization, and transport.
|
|
@@ -75,6 +133,7 @@ function nearestDeclaredKey(unexpected, declared) {
|
|
|
75
133
|
// accept arbitrary top-level keys (see extensionOptionsFromArgs), so unexpected
|
|
76
134
|
// keys there are by-design rather than typos and must not be flagged.
|
|
77
135
|
const UNEXPECTED_KEY_WARNING_EXEMPT_TOOLS = new Set(["pm_run"]);
|
|
136
|
+
const MCP_RETRY_ARGUMENT_KEYS = new Set(["inputResponses", "requestState"]);
|
|
78
137
|
function detectUnexpectedTopLevelKeys(toolName, args, toolDeclaredKeys = declaredToolKeys()) {
|
|
79
138
|
if (typeof args !== "object" || args === null || Array.isArray(args)) {
|
|
80
139
|
return [];
|
|
@@ -86,7 +145,7 @@ function detectUnexpectedTopLevelKeys(toolName, args, toolDeclaredKeys = declare
|
|
|
86
145
|
if (declared === undefined) {
|
|
87
146
|
return [];
|
|
88
147
|
}
|
|
89
|
-
const declaredSet = new Set(declared);
|
|
148
|
+
const declaredSet = new Set([...declared, ...MCP_RETRY_ARGUMENT_KEYS]);
|
|
90
149
|
const warnings = [];
|
|
91
150
|
for (const key of Object.keys(args)) {
|
|
92
151
|
if (declaredSet.has(key)) {
|
|
@@ -265,10 +324,8 @@ function boundMcpClientProvenance(value) {
|
|
|
265
324
|
}
|
|
266
325
|
return bounded;
|
|
267
326
|
}
|
|
268
|
-
function readMcpClientInfo(
|
|
269
|
-
const clientInfo = isRuntimeRecord(
|
|
270
|
-
? params.clientInfo
|
|
271
|
-
: {};
|
|
327
|
+
function readMcpClientInfo(value) {
|
|
328
|
+
const clientInfo = isRuntimeRecord(value) ? value : {};
|
|
272
329
|
const clientName = typeof clientInfo.name === "string" ? clientInfo.name.trim() : "";
|
|
273
330
|
if (clientName.length === 0)
|
|
274
331
|
return undefined;
|
|
@@ -292,11 +349,20 @@ function readMcpClientInfo(params) {
|
|
|
292
349
|
result.episode = episode;
|
|
293
350
|
return result;
|
|
294
351
|
}
|
|
295
|
-
async function
|
|
352
|
+
async function emitMcpChangeNotifications(action) {
|
|
353
|
+
if (isMutationAction(action ?? "")) {
|
|
354
|
+
await Promise.all([...PM_MCP_SUBSCRIPTIONS.values()].flatMap(({ registry }) => PM_MCP_RESOURCE_CONTRACTS.map((resource) => registry.emitResourceUpdated(resource.uri))));
|
|
355
|
+
}
|
|
356
|
+
if (["extension", "install", "package", "uninstall"].includes(action ?? "")) {
|
|
357
|
+
await Promise.all([...PM_MCP_SUBSCRIPTIONS.values()].map(({ registry }) => registry.emitListChanged("tools")));
|
|
358
|
+
}
|
|
359
|
+
pruneClosedMcpSubscriptionRegistries();
|
|
360
|
+
}
|
|
361
|
+
async function handleToolCall(paramsInput, clientInfo) {
|
|
296
362
|
return runWithHarnessDetectionSignals({
|
|
297
363
|
env: process.env,
|
|
298
364
|
argv: process.argv,
|
|
299
|
-
...(
|
|
365
|
+
...(clientInfo ? { client_info: clientInfo } : {}),
|
|
300
366
|
}, async () => {
|
|
301
367
|
const params = asRecordClone(paramsInput);
|
|
302
368
|
const name = readRequiredString(params, "name");
|
|
@@ -306,7 +372,16 @@ async function handleToolCall(paramsInput) {
|
|
|
306
372
|
if (!handler) {
|
|
307
373
|
throw new PmCliError(`Unknown pm MCP tool: ${name}`, 64);
|
|
308
374
|
}
|
|
309
|
-
const
|
|
375
|
+
const inputResponses = params.inputResponses === undefined
|
|
376
|
+
? undefined
|
|
377
|
+
: parseMcpInputResponses(params.inputResponses);
|
|
378
|
+
const requestedArgs = {
|
|
379
|
+
...decodeHtmlEntitiesInOptions(asRecordClone(params.arguments)),
|
|
380
|
+
...(inputResponses ? { inputResponses } : {}),
|
|
381
|
+
...(typeof params.requestState === "string"
|
|
382
|
+
? { requestState: params.requestState }
|
|
383
|
+
: {}),
|
|
384
|
+
};
|
|
310
385
|
const access = await resolveMcpToolAccess(TOOLS, name, requestedArgs);
|
|
311
386
|
if (!access.available) {
|
|
312
387
|
throw new PmCliError(`pm MCP tool "${name}" is unavailable in the ${access.profile} profile.`, 64);
|
|
@@ -339,10 +414,70 @@ async function handleToolCall(paramsInput) {
|
|
|
339
414
|
// cwd is applied inside the serialized activation cycle (see withActiveExtensions),
|
|
340
415
|
// so the chdir/restore is exclusive per request and cannot race a concurrent caller.
|
|
341
416
|
const result = await handler(args);
|
|
417
|
+
void emitMcpChangeNotifications(action);
|
|
342
418
|
return resultContent(result, warnings, args.tokenAccounting === true);
|
|
343
419
|
}, { probesEnabled: workspaceIdentity?.probes_enabled });
|
|
344
420
|
});
|
|
345
421
|
}
|
|
422
|
+
/** Open one modern subscription through a concrete transport-owned sink. */
|
|
423
|
+
export async function openMcpSubscription(input) {
|
|
424
|
+
if ((typeof input.request.id !== "string" &&
|
|
425
|
+
typeof input.request.id !== "number") ||
|
|
426
|
+
input.request.method !== "subscriptions/listen") {
|
|
427
|
+
throw new PmMcpProtocolError("Invalid MCP subscriptions/listen request", PM_MCP_ERROR_CODES.invalidParams, { required: ["id", "method", "params.notifications"] });
|
|
428
|
+
}
|
|
429
|
+
resolveMcpRequestContext(input.request.params);
|
|
430
|
+
const key = input.key ?? input.request.id;
|
|
431
|
+
if (PM_MCP_SUBSCRIPTIONS.has(key)) {
|
|
432
|
+
throw new PmMcpProtocolError("MCP subscription id is already active", PM_MCP_ERROR_CODES.invalidParams, { subscriptionId: input.request.id });
|
|
433
|
+
}
|
|
434
|
+
const registry = createMcpSubscriptionRegistry();
|
|
435
|
+
await registry.open({
|
|
436
|
+
id: input.request.id,
|
|
437
|
+
notifications: input.request.params?.notifications,
|
|
438
|
+
sink: input.sink,
|
|
439
|
+
});
|
|
440
|
+
PM_MCP_SUBSCRIPTIONS.set(key, { id: input.request.id, registry });
|
|
441
|
+
}
|
|
442
|
+
/** Gracefully close one active subscription by its request identifier. */
|
|
443
|
+
export function closeMcpSubscription(id, key = id) {
|
|
444
|
+
const subscription = PM_MCP_SUBSCRIPTIONS.get(key);
|
|
445
|
+
if (!subscription || subscription.id !== id)
|
|
446
|
+
return undefined;
|
|
447
|
+
PM_MCP_SUBSCRIPTIONS.delete(key);
|
|
448
|
+
return subscription.registry.close(id);
|
|
449
|
+
}
|
|
450
|
+
function closeStdioMcpSubscriptions() {
|
|
451
|
+
const closed = [];
|
|
452
|
+
for (const [key, subscription] of PM_MCP_SUBSCRIPTIONS) {
|
|
453
|
+
if (key !== subscription.id)
|
|
454
|
+
continue;
|
|
455
|
+
const result = closeMcpSubscription(subscription.id, key);
|
|
456
|
+
if (result)
|
|
457
|
+
closed.push({ id: subscription.id, result });
|
|
458
|
+
}
|
|
459
|
+
return closed;
|
|
460
|
+
}
|
|
461
|
+
/** Resolve the current workspace-enriched tool schema for HTTP header checks. */
|
|
462
|
+
export async function resolveMcpToolSchemaForRequest(request) {
|
|
463
|
+
if (request.method !== "tools/call")
|
|
464
|
+
return undefined;
|
|
465
|
+
const name = request.params?.name;
|
|
466
|
+
if (typeof name !== "string")
|
|
467
|
+
return undefined;
|
|
468
|
+
const surface = await resolveMcpToolSurface(TOOLS, requestWorkspaceArgs(request.params));
|
|
469
|
+
return surface.tools.find((tool) => tool.name === name)?.inputSchema;
|
|
470
|
+
}
|
|
471
|
+
/** Preserve MCP tool-result error semantics for non-protocol adapter failures. */
|
|
472
|
+
export function buildMcpToolCallErrorResult(request, error) {
|
|
473
|
+
if (request.method !== "tools/call" || error instanceof PmMcpProtocolError) {
|
|
474
|
+
return undefined;
|
|
475
|
+
}
|
|
476
|
+
const content = errorContent(error);
|
|
477
|
+
return hasMcpProtocolVersionMetadata(request)
|
|
478
|
+
? buildMcpCompleteResult(content, PM_MCP_SERVER_INFO)
|
|
479
|
+
: content;
|
|
480
|
+
}
|
|
346
481
|
function requestWorkspaceArgs(params) {
|
|
347
482
|
const paramsRecord = asRecordClone(params);
|
|
348
483
|
return asRecordClone(paramsRecord.arguments ?? paramsRecord);
|
|
@@ -411,62 +546,274 @@ function renderWorkflowPrompt(params) {
|
|
|
411
546
|
messages: [{ role: "user", content: { type: "text", text } }],
|
|
412
547
|
};
|
|
413
548
|
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
549
|
+
const LEGACY_MCP_ADAPTER = new LegacyMcpAdapter({
|
|
550
|
+
serverInfo: PM_MCP_SERVER_INFO,
|
|
551
|
+
capabilities: PM_MCP_SERVER_CAPABILITIES,
|
|
552
|
+
instructions: PM_MCP_INSTRUCTIONS,
|
|
553
|
+
parseClientInfo: readMcpClientInfo,
|
|
554
|
+
listTools: async (params) => ({
|
|
555
|
+
tools: (await resolveMcpToolSurface(TOOLS, requestWorkspaceArgs(params)))
|
|
556
|
+
.tools,
|
|
557
|
+
}),
|
|
558
|
+
callTool: handleToolCall,
|
|
559
|
+
listResources: () => ({ resources: PM_MCP_RESOURCE_CONTRACTS }),
|
|
560
|
+
readResource: (params) => readWorkspaceResource(readRequiredString(asRecordClone(params), "uri"), params),
|
|
561
|
+
listPrompts: () => ({
|
|
562
|
+
prompts: PM_MCP_PROMPT_CONTRACTS.map((prompt) => ({
|
|
563
|
+
name: prompt.name,
|
|
564
|
+
description: prompt.description,
|
|
565
|
+
arguments: prompt.arguments,
|
|
566
|
+
})),
|
|
567
|
+
}),
|
|
568
|
+
getPrompt: renderWorkflowPrompt,
|
|
569
|
+
});
|
|
570
|
+
function deterministicMcpTools(tools) {
|
|
571
|
+
return tools
|
|
572
|
+
.map((tool) => ({
|
|
573
|
+
...tool,
|
|
574
|
+
inputSchema: validateMcpJsonSchema(tool.inputSchema),
|
|
575
|
+
}))
|
|
576
|
+
.sort((left, right) => left.name.localeCompare(right.name));
|
|
577
|
+
}
|
|
578
|
+
function mcpTaskPrincipal(requestContext) {
|
|
579
|
+
return requestContext.clientInfo
|
|
580
|
+
? `${requestContext.clientInfo.name}@${requestContext.clientInfo.version}`
|
|
581
|
+
: "anonymous-stdio";
|
|
582
|
+
}
|
|
583
|
+
function mcpTaskStore() {
|
|
584
|
+
return createMcpTaskStore({
|
|
585
|
+
pmRoot: resolvePmRoot(process.cwd()),
|
|
586
|
+
owner: "pm-mcp-task-provider",
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
function shouldCreateMcpTask(params, requestContext) {
|
|
590
|
+
if (!hasMcpClientExtension(requestContext, PM_MCP_TASKS_EXTENSION)) {
|
|
591
|
+
return false;
|
|
418
592
|
}
|
|
419
|
-
|
|
420
|
-
|
|
593
|
+
const requestParams = asRecordClone(params);
|
|
594
|
+
const name = requestParams.name;
|
|
595
|
+
const args = asRecordClone(requestParams.arguments);
|
|
596
|
+
if (PM_MCP_DIRECT_TASK_TOOLS.has(name))
|
|
597
|
+
return true;
|
|
598
|
+
if (name === "pm_test" &&
|
|
599
|
+
(args.subcommand === "run" ||
|
|
600
|
+
(isMcpRecord(args.options) && args.options.subcommand === "run"))) {
|
|
601
|
+
return true;
|
|
421
602
|
}
|
|
422
|
-
if (
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
603
|
+
if (name !== "pm_run")
|
|
604
|
+
return false;
|
|
605
|
+
return (typeof args.action === "string" && PM_MCP_RUN_TASK_ACTIONS.has(args.action));
|
|
606
|
+
}
|
|
607
|
+
const PENDING_MCP_TASKS = new Map();
|
|
608
|
+
async function executeMcpTask(execution, inputResponses) {
|
|
609
|
+
const params = {
|
|
610
|
+
...execution.params,
|
|
611
|
+
...(inputResponses ? { inputResponses } : {}),
|
|
612
|
+
...(execution.requestState ? { requestState: execution.requestState } : {}),
|
|
613
|
+
};
|
|
614
|
+
try {
|
|
615
|
+
const result = await handleToolCall(params, execution.context.clientInfo);
|
|
616
|
+
await execution.store.complete(execution.taskId, execution.principal, buildMcpCompleteResult(result, PM_MCP_SERVER_INFO));
|
|
617
|
+
PENDING_MCP_TASKS.delete(execution.taskId);
|
|
618
|
+
}
|
|
619
|
+
catch (error) {
|
|
620
|
+
if (error instanceof PmMcpInputRequiredError) {
|
|
621
|
+
execution.requestState = error.requestState;
|
|
622
|
+
await execution.store.requireInput({
|
|
623
|
+
taskId: execution.taskId,
|
|
624
|
+
principal: execution.principal,
|
|
625
|
+
requestContext: execution.context,
|
|
626
|
+
inputRequests: error.inputRequests,
|
|
627
|
+
statusMessage: "Task requires additional client input.",
|
|
628
|
+
});
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
if (error instanceof PmMcpProtocolError) {
|
|
632
|
+
await execution.store.fail(execution.taskId, execution.principal, {
|
|
633
|
+
code: error.code,
|
|
634
|
+
message: error.message,
|
|
635
|
+
data: error.data,
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
else {
|
|
639
|
+
await execution.store.complete(execution.taskId, execution.principal, buildMcpCompleteResult(errorContent(error), PM_MCP_SERVER_INFO));
|
|
640
|
+
}
|
|
641
|
+
PENDING_MCP_TASKS.delete(execution.taskId);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
async function settleMcpTaskExecution(taskId, execution) {
|
|
645
|
+
try {
|
|
646
|
+
await execution;
|
|
647
|
+
}
|
|
648
|
+
catch {
|
|
649
|
+
// Persistence failures can outlive the request that created the detached
|
|
650
|
+
// execution. Keep them from becoming process-level unhandled rejections;
|
|
651
|
+
// the durable task record remains available for TTL-based recovery.
|
|
652
|
+
PENDING_MCP_TASKS.delete(taskId);
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
function scheduleMcpTaskExecution(execution, inputResponses) {
|
|
656
|
+
void settleMcpTaskExecution(execution.taskId, executeMcpTask(execution, inputResponses));
|
|
657
|
+
}
|
|
658
|
+
async function createMcpTaskForToolCall(params, requestContext) {
|
|
659
|
+
const store = mcpTaskStore();
|
|
660
|
+
const principal = mcpTaskPrincipal(requestContext);
|
|
661
|
+
const task = await store.create({
|
|
662
|
+
principal,
|
|
663
|
+
statusMessage: "pm operation accepted for asynchronous execution.",
|
|
664
|
+
});
|
|
665
|
+
const execution = {
|
|
666
|
+
context: requestContext,
|
|
667
|
+
params: asRecordClone(params),
|
|
668
|
+
principal,
|
|
669
|
+
store,
|
|
670
|
+
taskId: task.taskId,
|
|
671
|
+
};
|
|
672
|
+
PENDING_MCP_TASKS.set(task.taskId, execution);
|
|
673
|
+
scheduleMcpTaskExecution(execution);
|
|
674
|
+
return attachMcpServerInfo(task, PM_MCP_SERVER_INFO);
|
|
675
|
+
}
|
|
676
|
+
async function dispatchMcpTaskMethod(request, requestContext) {
|
|
677
|
+
if (!hasMcpClientExtension(requestContext, PM_MCP_TASKS_EXTENSION)) {
|
|
678
|
+
throw new PmMcpProtocolError("Missing required client capability", PM_MCP_ERROR_CODES.missingRequiredClientCapability, {
|
|
679
|
+
requiredCapabilities: {
|
|
680
|
+
extensions: { [PM_MCP_TASKS_EXTENSION]: {} },
|
|
430
681
|
},
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
682
|
+
});
|
|
683
|
+
}
|
|
684
|
+
const params = asRecordClone(request.params);
|
|
685
|
+
const taskId = readRequiredString(params, "taskId");
|
|
686
|
+
const principal = mcpTaskPrincipal(requestContext);
|
|
687
|
+
const store = mcpTaskStore();
|
|
688
|
+
if (request.method === "tasks/get") {
|
|
689
|
+
return buildMcpCompleteResult(await store.get(taskId, principal), PM_MCP_SERVER_INFO);
|
|
690
|
+
}
|
|
691
|
+
if (request.method === "tasks/update") {
|
|
692
|
+
const update = await store.update(taskId, principal, params.inputResponses);
|
|
693
|
+
const execution = PENDING_MCP_TASKS.get(taskId);
|
|
694
|
+
if (execution &&
|
|
695
|
+
update.acceptedKeys.length > 0 &&
|
|
696
|
+
update.remainingKeys.length === 0) {
|
|
697
|
+
scheduleMcpTaskExecution(execution, await store.takeInputResponses(taskId, principal));
|
|
698
|
+
}
|
|
699
|
+
return buildMcpCompleteResult({}, PM_MCP_SERVER_INFO);
|
|
442
700
|
}
|
|
701
|
+
await store.cancel(taskId, principal);
|
|
702
|
+
PENDING_MCP_TASKS.delete(taskId);
|
|
703
|
+
return buildMcpCompleteResult({}, PM_MCP_SERVER_INFO);
|
|
704
|
+
}
|
|
705
|
+
async function dispatchModernToolMethod(request, requestContext, clientInfo) {
|
|
443
706
|
if (request.method === "tools/list") {
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
707
|
+
const surface = await resolveMcpToolSurface(TOOLS, requestWorkspaceArgs(request.params));
|
|
708
|
+
return buildMcpCompleteResult(withMcpCachePolicy({ tools: deterministicMcpTools(surface.tools) }, PM_MCP_CACHE_POLICIES.tools), PM_MCP_SERVER_INFO);
|
|
709
|
+
}
|
|
710
|
+
if (shouldCreateMcpTask(request.params, requestContext)) {
|
|
711
|
+
return createMcpTaskForToolCall(request.params, requestContext);
|
|
447
712
|
}
|
|
448
|
-
|
|
449
|
-
return handleToolCall(request.params);
|
|
713
|
+
try {
|
|
714
|
+
return buildMcpCompleteResult(await handleToolCall(request.params, clientInfo), PM_MCP_SERVER_INFO);
|
|
715
|
+
}
|
|
716
|
+
catch (error) {
|
|
717
|
+
if (!(error instanceof PmMcpInputRequiredError))
|
|
718
|
+
throw error;
|
|
719
|
+
return buildMcpInputRequiredResult({
|
|
720
|
+
requestContext,
|
|
721
|
+
serverInfo: PM_MCP_SERVER_INFO,
|
|
722
|
+
inputRequests: error.inputRequests,
|
|
723
|
+
requestState: error.requestState,
|
|
724
|
+
});
|
|
450
725
|
}
|
|
726
|
+
}
|
|
727
|
+
async function dispatchModernResourceMethod(request) {
|
|
451
728
|
if (request.method === "resources/list") {
|
|
452
|
-
|
|
729
|
+
const resources = [...PM_MCP_RESOURCE_CONTRACTS].sort((left, right) => left.uri.localeCompare(right.uri));
|
|
730
|
+
return buildMcpCompleteResult(withMcpCachePolicy({ resources }, PM_MCP_CACHE_POLICIES.resources), PM_MCP_SERVER_INFO);
|
|
453
731
|
}
|
|
454
|
-
if (request.method === "resources/
|
|
455
|
-
return
|
|
732
|
+
if (request.method === "resources/templates/list") {
|
|
733
|
+
return buildMcpCompleteResult(withMcpCachePolicy({ resourceTemplates: [] }, PM_MCP_CACHE_POLICIES.resources), PM_MCP_SERVER_INFO);
|
|
456
734
|
}
|
|
457
|
-
|
|
458
|
-
|
|
735
|
+
try {
|
|
736
|
+
const resource = await readWorkspaceResource(readRequiredString(asRecordClone(request.params), "uri"), request.params);
|
|
737
|
+
return buildMcpCompleteResult(withMcpCachePolicy(resource, PM_MCP_CACHE_POLICIES.resource), PM_MCP_SERVER_INFO);
|
|
738
|
+
}
|
|
739
|
+
catch (error) {
|
|
740
|
+
if (!(error instanceof PmCliError) ||
|
|
741
|
+
!error.message.startsWith("Unknown pm MCP resource:")) {
|
|
742
|
+
throw error;
|
|
743
|
+
}
|
|
744
|
+
throw new PmMcpProtocolError(error.message, PM_MCP_ERROR_CODES.invalidParams, { field: "uri" });
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
function dispatchModernPromptMethod(request) {
|
|
748
|
+
const result = request.method === "prompts/list"
|
|
749
|
+
? withMcpCachePolicy({
|
|
459
750
|
prompts: PM_MCP_PROMPT_CONTRACTS.map((prompt) => ({
|
|
460
751
|
name: prompt.name,
|
|
461
752
|
description: prompt.description,
|
|
462
753
|
arguments: prompt.arguments,
|
|
463
|
-
})),
|
|
464
|
-
}
|
|
754
|
+
})).sort((left, right) => left.name.localeCompare(right.name)),
|
|
755
|
+
}, PM_MCP_CACHE_POLICIES.prompts)
|
|
756
|
+
: renderWorkflowPrompt(request.params);
|
|
757
|
+
return buildMcpCompleteResult(result, PM_MCP_SERVER_INFO);
|
|
758
|
+
}
|
|
759
|
+
/** Dispatch one validated stateless request through the modern surface. */
|
|
760
|
+
async function dispatchModernMcpRequest(request, meta) {
|
|
761
|
+
const requestContext = resolveMcpRequestContext(request.params);
|
|
762
|
+
const clientInfo = readMcpClientInfo(meta[PM_MCP_META_KEYS.clientInfo]);
|
|
763
|
+
if (request.method === "server/discover") {
|
|
764
|
+
return buildMcpDiscoverResult({
|
|
765
|
+
serverInfo: PM_MCP_SERVER_INFO,
|
|
766
|
+
capabilities: PM_MCP_SERVER_CAPABILITIES,
|
|
767
|
+
instructions: PM_MCP_INSTRUCTIONS,
|
|
768
|
+
});
|
|
769
|
+
}
|
|
770
|
+
if (request.method === "ping") {
|
|
771
|
+
// The preceding match is an mcp-deprecation-negative-control for modern callers.
|
|
772
|
+
throw new PmMcpProtocolError("MCP method not found: ping", -32601, {
|
|
773
|
+
removedIn: PM_MCP_PROTOCOL_VERSION,
|
|
774
|
+
});
|
|
465
775
|
}
|
|
466
|
-
if (request.method === "
|
|
467
|
-
|
|
776
|
+
if (request.method === "tasks/get" ||
|
|
777
|
+
request.method === "tasks/update" ||
|
|
778
|
+
request.method === "tasks/cancel") {
|
|
779
|
+
return dispatchMcpTaskMethod(request, requestContext);
|
|
468
780
|
}
|
|
469
|
-
|
|
781
|
+
if (request.method === "tools/list" || request.method === "tools/call") {
|
|
782
|
+
return dispatchModernToolMethod(request, requestContext, clientInfo);
|
|
783
|
+
}
|
|
784
|
+
if (request.method === "resources/list" ||
|
|
785
|
+
request.method === "resources/templates/list" ||
|
|
786
|
+
request.method === "resources/read") {
|
|
787
|
+
return dispatchModernResourceMethod(request);
|
|
788
|
+
}
|
|
789
|
+
if (request.method === "prompts/list" || request.method === "prompts/get") {
|
|
790
|
+
return dispatchModernPromptMethod(request);
|
|
791
|
+
}
|
|
792
|
+
throw new PmMcpProtocolError(`MCP method not found: ${request.method ?? "(missing)"}`, -32601);
|
|
793
|
+
}
|
|
794
|
+
/** Implements handle request for the public runtime surface of this module. */
|
|
795
|
+
async function handleRequestInReproducibleContext(request) {
|
|
796
|
+
if (!request.id && request.method?.startsWith("notifications/")) {
|
|
797
|
+
return undefined;
|
|
798
|
+
}
|
|
799
|
+
if (hasMcpProtocolVersionMetadata(request)) {
|
|
800
|
+
const allowlist = (process.env.PM_MCP_BAGGAGE_ALLOWLIST ?? "")
|
|
801
|
+
.split(",")
|
|
802
|
+
.map((entry) => entry.trim())
|
|
803
|
+
.filter(Boolean);
|
|
804
|
+
return runWithMcpTraceContext(extractMcpTraceContext(request.params, { baggageAllowlist: allowlist }), () => dispatchModernMcpRequest(request, request.params._meta));
|
|
805
|
+
}
|
|
806
|
+
if (request.method === "initialize") {
|
|
807
|
+
// The preceding match is an mcp-legacy-boundary delegated to the isolated adapter.
|
|
808
|
+
return LEGACY_MCP_ADAPTER.initialize(request.params);
|
|
809
|
+
}
|
|
810
|
+
return LEGACY_MCP_ADAPTER.dispatch(request);
|
|
811
|
+
}
|
|
812
|
+
/** Return whether a request explicitly supplies the modern version metadata key. */
|
|
813
|
+
function hasMcpProtocolVersionMetadata(request) {
|
|
814
|
+
const meta = isMcpRecord(request.params) ? request.params._meta : undefined;
|
|
815
|
+
return (isMcpRecord(meta) &&
|
|
816
|
+
Object.prototype.hasOwnProperty.call(meta, PM_MCP_META_KEYS.protocolVersion));
|
|
470
817
|
}
|
|
471
818
|
/** Dispatch one MCP request under supported process-level reproducibility settings. */
|
|
472
819
|
export async function handleRequest(request) {
|
|
@@ -476,11 +823,47 @@ function writeResponse(id, payload) {
|
|
|
476
823
|
process.stdout.write(`${JSON.stringify({ jsonrpc: "2.0", id, result: payload })}\n`);
|
|
477
824
|
}
|
|
478
825
|
function writeError(id, error) {
|
|
479
|
-
const code = error instanceof
|
|
826
|
+
const code = error instanceof PmMcpProtocolError
|
|
827
|
+
? error.code
|
|
828
|
+
: error instanceof PmCliError
|
|
829
|
+
? error.exitCode
|
|
830
|
+
: -32603;
|
|
480
831
|
const message = error instanceof Error ? error.message : String(error);
|
|
481
|
-
const data = error instanceof
|
|
832
|
+
const data = error instanceof PmMcpProtocolError
|
|
833
|
+
? error.data
|
|
834
|
+
: error instanceof PmCliError
|
|
835
|
+
? error.context
|
|
836
|
+
: undefined;
|
|
482
837
|
process.stdout.write(`${JSON.stringify({ jsonrpc: "2.0", id, error: { code, message, data } })}\n`);
|
|
483
838
|
}
|
|
839
|
+
/** Write an MCP tool execution failure as a successful tool result envelope. */
|
|
840
|
+
function writeToolCallErrorResponse(request, error) {
|
|
841
|
+
if (request.method !== "tools/call" || error instanceof PmMcpProtocolError) {
|
|
842
|
+
return false;
|
|
843
|
+
}
|
|
844
|
+
writeResponse(request.id, buildMcpToolCallErrorResult(request, error));
|
|
845
|
+
return true;
|
|
846
|
+
}
|
|
847
|
+
async function handleStdioTransportControl(request) {
|
|
848
|
+
if (request.method === "subscriptions/listen") {
|
|
849
|
+
await openMcpSubscription({
|
|
850
|
+
request,
|
|
851
|
+
sink: (notification) => {
|
|
852
|
+
process.stdout.write(`${JSON.stringify(notification)}\n`);
|
|
853
|
+
},
|
|
854
|
+
});
|
|
855
|
+
return true;
|
|
856
|
+
}
|
|
857
|
+
if (request.method !== "notifications/cancelled")
|
|
858
|
+
return false;
|
|
859
|
+
const requestId = request.params?.requestId;
|
|
860
|
+
if (typeof requestId === "string" || typeof requestId === "number") {
|
|
861
|
+
const result = closeMcpSubscription(requestId);
|
|
862
|
+
if (result)
|
|
863
|
+
writeResponse(requestId, result);
|
|
864
|
+
}
|
|
865
|
+
return true;
|
|
866
|
+
}
|
|
484
867
|
// pm-3puw: parse one JSON-RPC line, dispatch it, and write the response. Kept
|
|
485
868
|
// as a standalone async unit so the stdio loop can enqueue it onto a serial
|
|
486
869
|
// queue (process lines in arrival order) and tests can drive it directly.
|
|
@@ -506,6 +889,8 @@ async function processRpcLineWithHandler(line, requestHandler) {
|
|
|
506
889
|
}
|
|
507
890
|
const shouldRespond = Object.prototype.hasOwnProperty.call(request, "id");
|
|
508
891
|
try {
|
|
892
|
+
if (await handleStdioTransportControl(request))
|
|
893
|
+
return;
|
|
509
894
|
const result = await requestHandler(request);
|
|
510
895
|
if (shouldRespond && result !== undefined) {
|
|
511
896
|
writeResponse(request.id, result);
|
|
@@ -515,10 +900,7 @@ async function processRpcLineWithHandler(line, requestHandler) {
|
|
|
515
900
|
if (!shouldRespond) {
|
|
516
901
|
return;
|
|
517
902
|
}
|
|
518
|
-
if (request
|
|
519
|
-
writeResponse(request.id, errorContent(error));
|
|
520
|
-
}
|
|
521
|
-
else {
|
|
903
|
+
if (!writeToolCallErrorResponse(request, error)) {
|
|
522
904
|
writeError(request.id, error);
|
|
523
905
|
}
|
|
524
906
|
}
|
|
@@ -551,6 +933,13 @@ export function startMcpServer() {
|
|
|
551
933
|
rl.on("line", (line) => {
|
|
552
934
|
void queue.enqueue(() => processLine(line));
|
|
553
935
|
});
|
|
936
|
+
rl.on("close", () => {
|
|
937
|
+
void queue.enqueue(() => {
|
|
938
|
+
for (const closed of closeStdioMcpSubscriptions()) {
|
|
939
|
+
writeResponse(closed.id, closed.result);
|
|
940
|
+
}
|
|
941
|
+
});
|
|
942
|
+
});
|
|
554
943
|
}
|
|
555
944
|
// npm bin entries are symlinks (node_modules/.bin/pm-mcp -> dist/mcp/server.js),
|
|
556
945
|
// so argv[1] must be realpath-resolved before comparing against this module's
|
|
@@ -582,14 +971,19 @@ function readRuntimeTestHook(key) {
|
|
|
582
971
|
/** Public contract for test only, shared by SDK and presentation-layer consumers. */
|
|
583
972
|
export const _testOnly = {
|
|
584
973
|
boundMcpClientProvenance,
|
|
974
|
+
createMcpInputRequiredError: (input) => new PmMcpInputRequiredError(input),
|
|
975
|
+
createMcpProtocolError: (...input) => new PmMcpProtocolError(...input),
|
|
585
976
|
get closeManyOptionsFromFlat() {
|
|
586
977
|
return readRuntimeTestHook("closeManyOptionsFromFlat");
|
|
587
978
|
},
|
|
588
979
|
detectUnexpectedOptionKeys,
|
|
589
980
|
detectUnexpectedTopLevelKeys,
|
|
590
981
|
collectMutationGuardWarnings,
|
|
982
|
+
closeStdioMcpSubscriptions,
|
|
591
983
|
errorContent,
|
|
592
|
-
|
|
984
|
+
emitMcpChangeNotifications,
|
|
985
|
+
expireMcpSubscriptionRecord: (id, key = id) => PM_MCP_SUBSCRIPTIONS.get(key).registry.close(id),
|
|
986
|
+
getMcpClientInfo: () => LEGACY_MCP_ADAPTER.getClientInfo(),
|
|
593
987
|
get extensionOptionsFromArgs() {
|
|
594
988
|
return readRuntimeTestHook("extensionOptionsFromArgs");
|
|
595
989
|
},
|
|
@@ -600,7 +994,12 @@ export const _testOnly = {
|
|
|
600
994
|
return readRuntimeTestHook("mutationListOptions");
|
|
601
995
|
},
|
|
602
996
|
nearestDeclaredKey,
|
|
997
|
+
mcpTaskPrincipal,
|
|
603
998
|
resultContent,
|
|
999
|
+
resolveMcpRequestContext,
|
|
1000
|
+
shouldCreateMcpTask,
|
|
1001
|
+
settleMcpTaskExecution,
|
|
1002
|
+
subscriptionCount: () => PM_MCP_SUBSCRIPTIONS.size,
|
|
604
1003
|
get normalizeActionName() {
|
|
605
1004
|
return readRuntimeTestHook("normalizeActionName");
|
|
606
1005
|
},
|
|
@@ -649,4 +1048,4 @@ if (isInvokedAsMcpMainModule(process.argv[1], import.meta.url)) {
|
|
|
649
1048
|
}
|
|
650
1049
|
/* c8 ignore stop */
|
|
651
1050
|
//# sourceMappingURL=server.js.map
|
|
652
|
-
//# debugId=
|
|
1051
|
+
//# debugId=2621052e-5b3f-5ab2-b587-036f375ee187
|