mirror of
https://github.com/morluto/rea.git
synced 2026-11-23 15:05:51 +00:00
fix(mcp): retain doctor initialization when catalog requests fail (#13255)
This commit is contained in:
1 parent
1ed4471c59
commit
87889b8729
2 files changed
+97
-92
No files matched your search
+66
-62
@@ -92,25 +92,40 @@ interface McpDoctorCheck {
|
||||
readonly detail: string;
|
||||
}
|
||||
|
||||
const observedMcpServer = (client: Client) => {
|
||||
const identity = client.getServerVersion();
|
||||
return {
|
||||
name: identity?.name ?? null,
|
||||
version: identity?.version ?? null,
|
||||
protocol_version: client.getNegotiatedProtocolVersion() ?? null,
|
||||
};
|
||||
};
|
||||
|
||||
const failedMcpDoctorReport = (
|
||||
checks: readonly McpDoctorCheck[],
|
||||
server: ReturnType<typeof observedMcpServer> | null = null,
|
||||
) => ({
|
||||
healthy: false as const,
|
||||
adapter: "production-stdio" as const,
|
||||
server,
|
||||
inventory: null,
|
||||
identity: null,
|
||||
request_flow: { tool: "binary_session" as const, ok: false as const },
|
||||
checks,
|
||||
});
|
||||
|
||||
/** Diagnose the actual production stdio adapter with one startup-bounded SDK session. */
|
||||
export const runProductionMcpDoctor = async (options: McpDoctorOptions) => {
|
||||
const parsedBudgets = parseMcpStartupBudgets(options.environment);
|
||||
if (!parsedBudgets.ok)
|
||||
return {
|
||||
healthy: false as const,
|
||||
adapter: "production-stdio" as const,
|
||||
server: null,
|
||||
inventory: null,
|
||||
identity: null,
|
||||
request_flow: {
|
||||
tool: "binary_session" as const,
|
||||
ok: false as const,
|
||||
},
|
||||
checks: parsedBudgets.error.settings.map((issue) => ({
|
||||
name: issue.setting,
|
||||
ok: false as const,
|
||||
detail: issue.constraint,
|
||||
})),
|
||||
...failedMcpDoctorReport(
|
||||
parsedBudgets.error.settings.map((issue) => ({
|
||||
name: issue.setting,
|
||||
ok: false,
|
||||
detail: issue.constraint,
|
||||
})),
|
||||
),
|
||||
diagnostics: {
|
||||
stderr: "",
|
||||
stderr_bytes: 0,
|
||||
@@ -163,21 +178,9 @@ export const runProductionMcpDoctor = async (options: McpDoctorOptions) => {
|
||||
budget,
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
result = {
|
||||
healthy: false,
|
||||
adapter: "production-stdio" as const,
|
||||
server: null,
|
||||
inventory: null,
|
||||
identity: null,
|
||||
request_flow: { tool: "binary_session", ok: false },
|
||||
checks: [
|
||||
{
|
||||
name: "transport",
|
||||
ok: false,
|
||||
detail: doctorTransportDetail(cause, budget, controller.signal),
|
||||
},
|
||||
],
|
||||
};
|
||||
result = failedMcpDoctorReport([
|
||||
doctorTransportCheck(cause, budget, controller.signal),
|
||||
]);
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
// The owned transport is the only cleanup owner; Client.close would only
|
||||
@@ -207,20 +210,9 @@ export const runProductionMcpDoctor = async (options: McpDoctorOptions) => {
|
||||
return { ...result, diagnostics: stderr.value() };
|
||||
};
|
||||
|
||||
type ProductionMcpDoctorResult =
|
||||
| Awaited<ReturnType<typeof inspectProductionMcpSession>>
|
||||
| {
|
||||
readonly healthy: false;
|
||||
readonly adapter: "production-stdio";
|
||||
readonly server: null;
|
||||
readonly inventory: null;
|
||||
readonly identity: null;
|
||||
readonly request_flow: {
|
||||
readonly tool: "binary_session";
|
||||
readonly ok: false;
|
||||
};
|
||||
readonly checks: readonly McpDoctorCheck[];
|
||||
};
|
||||
type ProductionMcpDoctorResult = Awaited<
|
||||
ReturnType<typeof inspectProductionMcpSession>
|
||||
>;
|
||||
|
||||
/** The doctor's bounded stdio session, owning the process family it starts. */
|
||||
class DoctorStdioTransport extends OwnedMcpStdioTransport {
|
||||
@@ -263,15 +255,30 @@ const inspectProductionMcpSession = async (
|
||||
expectation: McpDoctorCatalogExpectation,
|
||||
budget: McpPhaseBudget,
|
||||
) => {
|
||||
const server = observedMcpServer(client);
|
||||
const initialization = {
|
||||
name: "initialize",
|
||||
ok:
|
||||
server.name === PRODUCT_IDENTITY.mcpServerKey &&
|
||||
server.version === PRODUCT_IDENTITY.packageVersion &&
|
||||
server.protocol_version !== null,
|
||||
detail: `${server.name ?? "unknown"}@${server.version ?? "unknown"}; protocol=${server.protocol_version ?? "unknown"}`,
|
||||
};
|
||||
const request = requestOptions(deadline, signal);
|
||||
// The SDK logs to stdout when listPrompts is called without the advertised
|
||||
// capability, which corrupts the doctor's structured output. An absent
|
||||
// capability is an empty prompt inventory for this diagnostic.
|
||||
const promptsRequest = listPromptsWhenAdvertised(client, request);
|
||||
const [tools, prompts] = await Promise.all([
|
||||
const catalogs = await Promise.all([
|
||||
client.listTools(undefined, request),
|
||||
promptsRequest,
|
||||
]);
|
||||
listPromptsWhenAdvertised(client, request),
|
||||
]).then(ok, (cause: unknown) => err(cause));
|
||||
if (!catalogs.ok)
|
||||
return failedMcpDoctorReport(
|
||||
[initialization, doctorTransportCheck(catalogs.error, budget, signal)],
|
||||
server,
|
||||
);
|
||||
|
||||
const [tools, prompts] = catalogs.value;
|
||||
const sessionTool = tools.tools.find(({ name }) => name === "binary_session");
|
||||
// The reply is checked against the canonical output contract, not the
|
||||
// listing: the default output profile omits output schemas and `compact`
|
||||
@@ -315,17 +322,8 @@ const inspectProductionMcpSession = async (
|
||||
requestFlow.ok ? requestFlow.value.structuredContent : undefined,
|
||||
);
|
||||
const { catalog } = expectation;
|
||||
const serverVersion = client.getServerVersion();
|
||||
const protocolVersion = client.getNegotiatedProtocolVersion();
|
||||
const checks: McpDoctorCheck[] = [
|
||||
{
|
||||
name: "initialize",
|
||||
ok:
|
||||
serverVersion?.name === PRODUCT_IDENTITY.mcpServerKey &&
|
||||
serverVersion.version === PRODUCT_IDENTITY.packageVersion &&
|
||||
protocolVersion !== undefined,
|
||||
detail: `${serverVersion?.name ?? "unknown"}@${serverVersion?.version ?? "unknown"}; protocol=${protocolVersion ?? "unknown"}`,
|
||||
},
|
||||
initialization,
|
||||
...Object.entries(inventory).map(([name, comparison]) =>
|
||||
inventoryCheck(name, comparison),
|
||||
),
|
||||
@@ -350,11 +348,7 @@ const inspectProductionMcpSession = async (
|
||||
return {
|
||||
healthy: checks.every(({ ok }) => ok),
|
||||
adapter: "production-stdio" as const,
|
||||
server: {
|
||||
name: serverVersion?.name ?? null,
|
||||
version: serverVersion?.version ?? null,
|
||||
protocol_version: protocolVersion ?? null,
|
||||
},
|
||||
server,
|
||||
inventory,
|
||||
identity: {
|
||||
expected_package_version: PRODUCT_IDENTITY.packageVersion,
|
||||
@@ -713,3 +707,13 @@ const doctorTransportDetail = (
|
||||
(cause instanceof SdkError && cause.code === SdkErrorCode.RequestTimeout);
|
||||
return timedOut ? `${message} ${mcpDoctorDeadlineRecovery(budget)}` : message;
|
||||
};
|
||||
|
||||
const doctorTransportCheck = (
|
||||
cause: unknown,
|
||||
budget: McpPhaseBudget,
|
||||
signal: AbortSignal,
|
||||
): McpDoctorCheck => ({
|
||||
name: "transport",
|
||||
ok: false,
|
||||
detail: doctorTransportDetail(cause, budget, signal),
|
||||
});
|
||||
@@ -47,14 +47,8 @@ import {
|
||||
import { parseConfig } from "../../../src/config/parseConfig.js";
|
||||
import { runSessionStatus } from "../../../src/application/DirectAnalysisStatus.js";
|
||||
import { ok } from "../../../src/domain/result.js";
|
||||
import {
|
||||
resolveMcpStartupBudgets,
|
||||
MCP_PHASE_BUDGET_FLOOR_MS,
|
||||
} from "../../../src/config/mcpStartupBudgets.js";
|
||||
import {
|
||||
mcpDoctorDeadlineRecovery,
|
||||
MCP_STARTUP_POLICY,
|
||||
} from "../../../src/config/mcpStartupPolicy.js";
|
||||
import { MCP_PHASE_BUDGET_FLOOR_MS } from "../../../src/config/mcpStartupBudgets.js";
|
||||
import { MCP_STARTUP_POLICY } from "../../../src/config/mcpStartupPolicy.js";
|
||||
import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js";
|
||||
import { waitFor } from "../../support/testWait.js";
|
||||
import { COMPACT_INPUT_SCHEMA_PRESENTATION } from "../../../src/contracts/mcpSchemaPresentation.js";
|
||||
@@ -96,13 +90,6 @@ const expectDoctorDeadline = (
|
||||
detail: string | undefined,
|
||||
milliseconds: number,
|
||||
) => {
|
||||
expect(detail).toContain(
|
||||
mcpDoctorDeadlineRecovery(
|
||||
resolveMcpStartupBudgets({
|
||||
REA_MCP_DOCTOR_DEADLINE_MS: String(milliseconds),
|
||||
}).doctor,
|
||||
),
|
||||
);
|
||||
expect(detail).toContain("REA_MCP_DOCTOR_DEADLINE_MS");
|
||||
expect(detail).toContain(`${String(milliseconds)} ms`);
|
||||
expect(detail).toContain("source setting");
|
||||
@@ -909,31 +896,45 @@ describe("production MCP doctor request-flow deadline", () => {
|
||||
});
|
||||
|
||||
it.each([
|
||||
"unrelated fixture method refusal",
|
||||
"Request timed out while reading a remote fixture",
|
||||
"Maximum total timeout exceeded in a remote operation",
|
||||
"Production MCP doctor deadline expired is remote diagnostic text",
|
||||
])(
|
||||
"does not infer the doctor deadline from remote failure prose: %s",
|
||||
async (message) => {
|
||||
[
|
||||
"refuse-list",
|
||||
"transport",
|
||||
"Request timed out while reading a remote fixture",
|
||||
],
|
||||
[
|
||||
"refuse-call",
|
||||
"request-flow",
|
||||
"Maximum total timeout exceeded in a remote operation",
|
||||
],
|
||||
["refuse-call", "request-flow", "Production MCP doctor deadline expired"],
|
||||
] as const)(
|
||||
"keeps initialization evidence when %s reports %s: %s",
|
||||
async (mode, failedCheck, message) => {
|
||||
const result = await runCachedMcpDoctor({
|
||||
command: process.execPath,
|
||||
args: [resolve("tests/fixtures/mcpStartupProbeChild.mjs")],
|
||||
cwd: process.cwd(),
|
||||
environment: {
|
||||
...process.env,
|
||||
REA_MCP_PROBE_MODE: "refuse-call",
|
||||
REA_MCP_PROBE_MODE: mode,
|
||||
REA_MCP_PROBE_ERROR_MESSAGE: message,
|
||||
REA_MCP_DOCTOR_DEADLINE_MS: "5000",
|
||||
},
|
||||
});
|
||||
expect(result.server).not.toBeNull();
|
||||
const requestFlow = result.checks.find(
|
||||
(check) => check.name === "request-flow",
|
||||
);
|
||||
expect(requestFlow?.ok).toBe(false);
|
||||
expect(requestFlow?.detail).toContain(message);
|
||||
expect(requestFlow?.detail).not.toContain("REA_MCP_DOCTOR_DEADLINE_MS");
|
||||
expect(result.server).toEqual({
|
||||
name: "probe-fixture",
|
||||
version: "1",
|
||||
protocol_version: expect.any(String),
|
||||
});
|
||||
expect(result.checks).toContainEqual({
|
||||
name: "initialize",
|
||||
ok: false,
|
||||
detail: `probe-fixture@1; protocol=${result.server?.protocol_version}`,
|
||||
});
|
||||
const refusal = result.checks.find(({ name }) => name === failedCheck);
|
||||
expect(refusal?.ok).toBe(false);
|
||||
expect(refusal?.detail).toContain(message);
|
||||
expect(refusal?.detail).not.toContain("REA_MCP_DOCTOR_DEADLINE_MS");
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
Reference in new issue
Block a user