co-maintainer 0.7.10-beta.2 → 0.7.11-beta.2
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/dist/package.json +1 -1
- package/dist/src/cli/commands/registry.js +12 -0
- package/dist/src/cli/commands/set.js +60 -4
- package/dist/src/config.d.ts +7 -0
- package/dist/src/local/review_local.js +8 -2
- package/dist/src/pr/review_tools.d.ts +26 -4
- package/dist/src/pr/review_tools.js +45 -5
- package/dist/src/pr/reviewer.d.ts +7 -2
- package/dist/src/pr/reviewer.js +79 -15
- package/dist/src/review/private_text.d.ts +26 -0
- package/dist/src/review/private_text.js +131 -0
- package/dist/src/services/remote_review.js +4 -1
- package/dist/src/services/review.d.ts +8 -0
- package/dist/src/services/review.js +13 -2
- package/dist/src/tools/mcp_client.d.ts +81 -0
- package/dist/src/tools/mcp_client.js +259 -0
- package/dist/src/tools/mcp_config.d.ts +26 -0
- package/dist/src/tools/mcp_config.js +111 -0
- package/dist/src/tools/mcp_tools.d.ts +39 -0
- package/dist/src/tools/mcp_tools.js +150 -0
- package/dist/src/tools/search_non_code.d.ts +8 -0
- package/dist/src/tools/search_non_code.js +17 -7
- package/dist/src/util/runtime.d.ts +11 -1
- package/dist/src/util/runtime.js +34 -5
- package/package.json +1 -1
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/** Said in the review prompt when the model is offered an MCP tool. */
|
|
2
|
+
export const MCP_PRIVATE_GUIDANCE = "Results from mcp- tools are private. Base a finding on them when they show the change is wrong, and say only that a private source of this repository requires it. Never quote, paraphrase or summarize their text in a finding.";
|
|
3
|
+
/** What a removed passage becomes. */
|
|
4
|
+
export const PRIVATE_TEXT_REMOVED = "[private context removed]";
|
|
5
|
+
/** The shortest run of words that counts as a copied passage. */
|
|
6
|
+
export const PRIVATE_RUN_WORDS = 8;
|
|
7
|
+
/** Words are runs of letters and digits, so punctuation and case never
|
|
8
|
+
* decide a match. */
|
|
9
|
+
function words(text) {
|
|
10
|
+
const out = [];
|
|
11
|
+
for (const match of text.matchAll(/[\p{L}\p{N}]+/gu)) {
|
|
12
|
+
out.push({
|
|
13
|
+
word: match[0].toLowerCase(),
|
|
14
|
+
start: match.index,
|
|
15
|
+
end: match.index + match[0].length,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
return out;
|
|
19
|
+
}
|
|
20
|
+
function runsOf(text) {
|
|
21
|
+
const list = words(text).map((item) => item.word);
|
|
22
|
+
const runs = new Set();
|
|
23
|
+
for (let i = 0; i + PRIVATE_RUN_WORDS <= list.length; i++) {
|
|
24
|
+
runs.add(list.slice(i, i + PRIVATE_RUN_WORDS).join(" "));
|
|
25
|
+
}
|
|
26
|
+
return runs;
|
|
27
|
+
}
|
|
28
|
+
/** One piece of text with every copied run replaced. Overlapping and
|
|
29
|
+
* touching runs make one removal. */
|
|
30
|
+
function scrub(text, runs) {
|
|
31
|
+
const list = words(text);
|
|
32
|
+
const covered = Array.from({ length: list.length }, () => false);
|
|
33
|
+
for (let i = 0; i + PRIVATE_RUN_WORDS <= list.length; i++) {
|
|
34
|
+
const key = list
|
|
35
|
+
.slice(i, i + PRIVATE_RUN_WORDS)
|
|
36
|
+
.map((item) => item.word)
|
|
37
|
+
.join(" ");
|
|
38
|
+
if (!runs.has(key))
|
|
39
|
+
continue;
|
|
40
|
+
for (let j = i; j < i + PRIVATE_RUN_WORDS; j++)
|
|
41
|
+
covered[j] = true;
|
|
42
|
+
}
|
|
43
|
+
let out = "";
|
|
44
|
+
let at = 0;
|
|
45
|
+
let removed = 0;
|
|
46
|
+
for (let i = 0; i < list.length; i++) {
|
|
47
|
+
if (!covered[i])
|
|
48
|
+
continue;
|
|
49
|
+
let last = i;
|
|
50
|
+
while (last + 1 < list.length && covered[last + 1])
|
|
51
|
+
last++;
|
|
52
|
+
out += text.slice(at, list[i].start) + PRIVATE_TEXT_REMOVED;
|
|
53
|
+
at = list[last].end;
|
|
54
|
+
removed++;
|
|
55
|
+
i = last;
|
|
56
|
+
}
|
|
57
|
+
return { text: out + text.slice(at), removed };
|
|
58
|
+
}
|
|
59
|
+
/** Lines the check leaves alone: the rendered heading, location, references
|
|
60
|
+
* and suggestion lines, which hold paths and ids written by code. */
|
|
61
|
+
const STRUCTURE = /^(#{1,6} |Location: |References: |Suggestion: )/;
|
|
62
|
+
/** The review's Markdown with every copied passage replaced, and how many
|
|
63
|
+
* were. A paragraph is checked as one piece, so a passage that wraps a line
|
|
64
|
+
* is still found. A suggestion block is the author's code with a fix and is
|
|
65
|
+
* left as it is. Inside any other fenced block each line is checked on its
|
|
66
|
+
* own, so the block keeps its lines. */
|
|
67
|
+
export function removePrivateText(markdown, privateResults) {
|
|
68
|
+
const runs = new Set();
|
|
69
|
+
for (const result of privateResults) {
|
|
70
|
+
for (const run of runsOf(result))
|
|
71
|
+
runs.add(run);
|
|
72
|
+
}
|
|
73
|
+
if (runs.size === 0)
|
|
74
|
+
return { text: markdown, removed: 0 };
|
|
75
|
+
const lines = markdown.split("\n");
|
|
76
|
+
const out = [];
|
|
77
|
+
let removed = 0;
|
|
78
|
+
let paragraph = [];
|
|
79
|
+
const flush = () => {
|
|
80
|
+
if (paragraph.length === 0)
|
|
81
|
+
return;
|
|
82
|
+
const result = scrub(paragraph.join("\n"), runs);
|
|
83
|
+
removed += result.removed;
|
|
84
|
+
out.push(result.text);
|
|
85
|
+
paragraph = [];
|
|
86
|
+
};
|
|
87
|
+
let fence;
|
|
88
|
+
for (const line of lines) {
|
|
89
|
+
const fenceLine = /^\s*```/.test(line);
|
|
90
|
+
if (fence) {
|
|
91
|
+
if (fenceLine) {
|
|
92
|
+
fence = undefined;
|
|
93
|
+
out.push(line);
|
|
94
|
+
}
|
|
95
|
+
else if (fence === "suggestion") {
|
|
96
|
+
out.push(line);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
const result = scrub(line, runs);
|
|
100
|
+
removed += result.removed;
|
|
101
|
+
out.push(result.text);
|
|
102
|
+
}
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (fenceLine) {
|
|
106
|
+
flush();
|
|
107
|
+
fence = /^\s*```suggestion\b/.test(line) ? "suggestion" : "other";
|
|
108
|
+
out.push(line);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (line.trim() === "" || STRUCTURE.test(line)) {
|
|
112
|
+
flush();
|
|
113
|
+
out.push(line);
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
paragraph.push(line);
|
|
117
|
+
}
|
|
118
|
+
flush();
|
|
119
|
+
return { text: out.join("\n"), removed };
|
|
120
|
+
}
|
|
121
|
+
/** `private_text_removed`, or nothing when no passage was removed. */
|
|
122
|
+
export function privateTextWarning(removed) {
|
|
123
|
+
if (removed === 0)
|
|
124
|
+
return [];
|
|
125
|
+
return [
|
|
126
|
+
{
|
|
127
|
+
code: "private_text_removed",
|
|
128
|
+
message: `${removed} ${removed === 1 ? "passage" : "passages"} copied from a private MCP result ${removed === 1 ? "was" : "were"} removed from this review.`,
|
|
129
|
+
},
|
|
130
|
+
];
|
|
131
|
+
}
|
|
@@ -360,7 +360,9 @@ export function registerRemoteReviewHandler() {
|
|
|
360
360
|
extras.carryPrompt = buildCarryPromptSection(carryItems, revision);
|
|
361
361
|
}
|
|
362
362
|
}
|
|
363
|
-
// The client runs codegraph and search in its own checkout.
|
|
363
|
+
// The client runs codegraph and search in its own checkout. The
|
|
364
|
+
// server runs the MCP servers it has for the repository, as it runs
|
|
365
|
+
// its own extensions.
|
|
364
366
|
extras.tools = {
|
|
365
367
|
kind: "bridge",
|
|
366
368
|
tools: remoteBridgeToolHandlers(job.id, signal, allowed, [
|
|
@@ -371,6 +373,7 @@ export function registerRemoteReviewHandler() {
|
|
|
371
373
|
run: () => "",
|
|
372
374
|
},
|
|
373
375
|
]),
|
|
376
|
+
mcp: true,
|
|
374
377
|
};
|
|
375
378
|
const started = performance.now();
|
|
376
379
|
let tokensIn = 0;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type ParsedFinding } from "../pr/findings.ts";
|
|
2
2
|
import { type Anchor } from "../pr/hunks.ts";
|
|
3
|
+
import type { ToolUsage } from "../ai/tool_gate.ts";
|
|
3
4
|
import { type Coverage } from "../review/coverage.ts";
|
|
4
5
|
import { type ReviewBlocking } from "../review/blocking.ts";
|
|
5
6
|
import { type LogFn } from "./jobs.ts";
|
|
@@ -51,7 +52,14 @@ export interface ReviewMetadata {
|
|
|
51
52
|
coverage?: Coverage;
|
|
52
53
|
/** Whether the next round sends the files this round dropped. */
|
|
53
54
|
catchesUp?: boolean;
|
|
55
|
+
/** What the review's tools did, for the Tools line. */
|
|
56
|
+
tools?: ToolUsage;
|
|
57
|
+
/** The MCP servers the review ran, by name only. */
|
|
58
|
+
mcpServers?: string[];
|
|
54
59
|
}
|
|
60
|
+
/** `- Tools: 11 calls · MCP: tracker`, or nothing when no tool ran and no
|
|
61
|
+
* MCP server did. Server names only: what a server returned is private. */
|
|
62
|
+
export declare function toolsMetadataLine(tools: ToolUsage | undefined, mcpServers?: string[]): string;
|
|
55
63
|
export declare function formatDuration(ms: number): string;
|
|
56
64
|
/** A finding an earlier round reported that this round still finds, with the
|
|
57
65
|
* commit that first found it when known. */
|
|
@@ -176,6 +176,14 @@ export function appReviewEvent(findings, mode = DEFAULT_REVIEW_BLOCKING) {
|
|
|
176
176
|
const blocking = open.some((row) => isBlocking(mode, { severity: row.severity, text: row.title }));
|
|
177
177
|
return reviewEvent(blocking ? 1 : 0);
|
|
178
178
|
}
|
|
179
|
+
/** `- Tools: 11 calls · MCP: tracker`, or nothing when no tool ran and no
|
|
180
|
+
* MCP server did. Server names only: what a server returned is private. */
|
|
181
|
+
export function toolsMetadataLine(tools, mcpServers = []) {
|
|
182
|
+
const calls = tools?.calls ?? 0;
|
|
183
|
+
if (calls === 0 && mcpServers.length === 0)
|
|
184
|
+
return "";
|
|
185
|
+
return `- Tools: ${calls} ${calls === 1 ? "call" : "calls"}${mcpServers.length ? ` · MCP: ${mcpServers.join(", ")}` : ""}\n`;
|
|
186
|
+
}
|
|
179
187
|
export function formatDuration(ms) {
|
|
180
188
|
const totalSeconds = Math.max(0, Math.round(ms / 1000));
|
|
181
189
|
const hours = Math.floor(totalSeconds / 3600);
|
|
@@ -187,13 +195,14 @@ export function formatDuration(ms) {
|
|
|
187
195
|
return `${minutes}m ${seconds}s`;
|
|
188
196
|
return `${seconds}s`;
|
|
189
197
|
}
|
|
190
|
-
function metadataBlock({ jobId, model, durationMs, coverage, }) {
|
|
198
|
+
function metadataBlock({ jobId, model, durationMs, coverage, tools, mcpServers, }) {
|
|
191
199
|
return (`\n\n<details>\n<summary>Metadata</summary>\n\n` +
|
|
192
200
|
`- Activity: ${jobId}\n` +
|
|
193
201
|
`- co-maintainer version: ${VERSION}\n` +
|
|
194
202
|
`- Model: ${model}\n` +
|
|
195
203
|
`- Duration: ${formatDuration(durationMs)}\n` +
|
|
196
204
|
(coverage ? `- Coverage: ${coverageCounts(coverage)}\n` : "") +
|
|
205
|
+
toolsMetadataLine(tools, mcpServers) +
|
|
197
206
|
`</details>`);
|
|
198
207
|
}
|
|
199
208
|
/** The files this round never sent to the model, as a list under the
|
|
@@ -834,7 +843,7 @@ async function runReviewJobCore(job, log, client, ai, signal) {
|
|
|
834
843
|
// Only an incremental round leaves out what the last round saw, so only
|
|
835
844
|
// it sends the files the last round dropped.
|
|
836
845
|
const catchesUp = scope === "incremental";
|
|
837
|
-
const extras = { catchesUp, signal };
|
|
846
|
+
const extras = { catchesUp, signal, mcpForkRule: true };
|
|
838
847
|
// A guide rebuilt after the previous review invalidates that review's
|
|
839
848
|
// verdicts and its "unchanged" suppression. Treat the run as
|
|
840
849
|
// if it had started fresh: carry-over off, every changed file back in scope,
|
|
@@ -1034,6 +1043,8 @@ async function runReviewJobCore(job, log, client, ai, signal) {
|
|
|
1034
1043
|
durationMs: Date.now() - startedAt,
|
|
1035
1044
|
coverage: response.coverage,
|
|
1036
1045
|
catchesUp,
|
|
1046
|
+
tools: response.tools,
|
|
1047
|
+
mcpServers: response.mcpServers,
|
|
1037
1048
|
}, options.reviewBlocking, extensions);
|
|
1038
1049
|
await resolveFailureNote(github, job, reviewId, log);
|
|
1039
1050
|
const openFindings = totalFindings - closedCount + standingExtensionFindings(extensions);
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/** A hand-written MCP client over stdio (issue 79).
|
|
2
|
+
*
|
|
3
|
+
* It speaks newline-delimited JSON-RPC 2.0 and knows five messages:
|
|
4
|
+
* `initialize`, `notifications/initialized`, `tools/list`, `tools/call`, and
|
|
5
|
+
* an answer to the server's `ping`. HTTP transports and OAuth would be the
|
|
6
|
+
* only reason to add the SDK, and the project keeps no runtime dependency.
|
|
7
|
+
*
|
|
8
|
+
* The server process gets the `env` from its config plus `PATH` and `HOME`.
|
|
9
|
+
* co-maintainer's own tokens are never passed. What the server prints on
|
|
10
|
+
* stderr is kept only to say why a start failed, and is never sent anywhere
|
|
11
|
+
* else. */
|
|
12
|
+
import type { ChildProcess } from "node:child_process";
|
|
13
|
+
import type { McpServerConfig } from "./mcp_config.ts";
|
|
14
|
+
/** The protocol version co-maintainer asks for. A server that answers with
|
|
15
|
+
* another one is still used: the five messages are the same in each. */
|
|
16
|
+
export declare const MCP_PROTOCOL_VERSION = "2025-06-18";
|
|
17
|
+
/** Startup, `initialize` and `tools/list` together. */
|
|
18
|
+
export declare const MCP_STARTUP_MS = 10000;
|
|
19
|
+
export type McpTool = {
|
|
20
|
+
name: string;
|
|
21
|
+
description?: string;
|
|
22
|
+
inputSchema?: unknown;
|
|
23
|
+
annotations?: {
|
|
24
|
+
readOnlyHint?: unknown;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
/** One `tools/call` answer: the text parts joined, and whether the server
|
|
28
|
+
* marked it as an error. */
|
|
29
|
+
export type McpCallResult = {
|
|
30
|
+
text: string;
|
|
31
|
+
isError: boolean;
|
|
32
|
+
};
|
|
33
|
+
/** Why a server cannot be used. The message is co-maintainer's own and never
|
|
34
|
+
* holds what the server sent. */
|
|
35
|
+
export declare class McpError extends Error {
|
|
36
|
+
}
|
|
37
|
+
/** The server answered a request with a JSON-RPC error. It is still running,
|
|
38
|
+
* so this is not `McpError`. */
|
|
39
|
+
export declare class McpRpcError extends Error {
|
|
40
|
+
}
|
|
41
|
+
/** The server's environment: its own `env`, plus `PATH` and `HOME`. */
|
|
42
|
+
export declare function mcpEnv(env: Record<string, string> | undefined): Record<string, string>;
|
|
43
|
+
export type Spawn = (command: string, options: {
|
|
44
|
+
args: string[];
|
|
45
|
+
env: Record<string, string>;
|
|
46
|
+
}) => ChildProcess;
|
|
47
|
+
export declare class McpClient {
|
|
48
|
+
private readonly child;
|
|
49
|
+
private buffer;
|
|
50
|
+
private nextId;
|
|
51
|
+
private readonly pending;
|
|
52
|
+
private ended;
|
|
53
|
+
private stderrTail;
|
|
54
|
+
private readonly exited;
|
|
55
|
+
private constructor();
|
|
56
|
+
/** Spawns the server, then `initialize`, `notifications/initialized` and
|
|
57
|
+
* `tools/list`, all within `timeoutMs`. A server that fails any of them is
|
|
58
|
+
* stopped before the error is thrown. */
|
|
59
|
+
static start(config: McpServerConfig, options?: {
|
|
60
|
+
timeoutMs?: number;
|
|
61
|
+
spawn?: Spawn;
|
|
62
|
+
}): Promise<{
|
|
63
|
+
client: McpClient;
|
|
64
|
+
tools: McpTool[];
|
|
65
|
+
}>;
|
|
66
|
+
/** What the server last printed on stderr, for a failed start's log line. */
|
|
67
|
+
stderr(): string;
|
|
68
|
+
get alive(): boolean;
|
|
69
|
+
/** One `tools/call`. Text content is kept, anything else is left out with
|
|
70
|
+
* a note. A timeout or an exit throws `McpError`. */
|
|
71
|
+
call(name: string, args: unknown, timeoutMs: number): Promise<McpCallResult>;
|
|
72
|
+
/** Closes stdin, which ends a well-behaved server, then SIGTERM, then
|
|
73
|
+
* SIGKILL. Never throws. */
|
|
74
|
+
stop(): Promise<void>;
|
|
75
|
+
private end;
|
|
76
|
+
private send;
|
|
77
|
+
private notify;
|
|
78
|
+
private request;
|
|
79
|
+
private read;
|
|
80
|
+
private receive;
|
|
81
|
+
}
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { commandPipe, getEnv } from "../util/runtime.js";
|
|
2
|
+
import { VERSION } from "../version.js";
|
|
3
|
+
/** The protocol version co-maintainer asks for. A server that answers with
|
|
4
|
+
* another one is still used: the five messages are the same in each. */
|
|
5
|
+
export const MCP_PROTOCOL_VERSION = "2025-06-18";
|
|
6
|
+
/** Startup, `initialize` and `tools/list` together. */
|
|
7
|
+
export const MCP_STARTUP_MS = 10_000;
|
|
8
|
+
/** A line longer than this is not a message anyone meant to send. */
|
|
9
|
+
const MAX_LINE_CHARS = 16 * 1024 * 1024;
|
|
10
|
+
/** The stderr tail kept for a failed start. */
|
|
11
|
+
const STDERR_TAIL_CHARS = 2000;
|
|
12
|
+
/** How long `stop` waits after closing stdin, then after SIGTERM. */
|
|
13
|
+
const STOP_GRACE_MS = 1000;
|
|
14
|
+
/** At most this many `tools/list` pages are read. */
|
|
15
|
+
const MAX_LIST_PAGES = 20;
|
|
16
|
+
/** Why a server cannot be used. The message is co-maintainer's own and never
|
|
17
|
+
* holds what the server sent. */
|
|
18
|
+
export class McpError extends Error {
|
|
19
|
+
}
|
|
20
|
+
/** The server answered a request with a JSON-RPC error. It is still running,
|
|
21
|
+
* so this is not `McpError`. */
|
|
22
|
+
export class McpRpcError extends Error {
|
|
23
|
+
}
|
|
24
|
+
/** The server's environment: its own `env`, plus `PATH` and `HOME`. */
|
|
25
|
+
export function mcpEnv(env) {
|
|
26
|
+
const out = {};
|
|
27
|
+
for (const name of ["PATH", "HOME"]) {
|
|
28
|
+
const value = getEnv(name);
|
|
29
|
+
if (value !== undefined)
|
|
30
|
+
out[name] = value;
|
|
31
|
+
}
|
|
32
|
+
return { ...out, ...env };
|
|
33
|
+
}
|
|
34
|
+
function isRecord(value) {
|
|
35
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
36
|
+
}
|
|
37
|
+
export class McpClient {
|
|
38
|
+
child;
|
|
39
|
+
buffer = "";
|
|
40
|
+
nextId = 1;
|
|
41
|
+
pending = new Map();
|
|
42
|
+
ended;
|
|
43
|
+
stderrTail = "";
|
|
44
|
+
exited;
|
|
45
|
+
constructor(child) {
|
|
46
|
+
this.child = child;
|
|
47
|
+
this.exited = new Promise((resolve) => {
|
|
48
|
+
child.once("close", () => resolve());
|
|
49
|
+
});
|
|
50
|
+
child.stdout?.setEncoding("utf8");
|
|
51
|
+
child.stdout?.on("data", (chunk) => this.read(chunk));
|
|
52
|
+
child.stderr?.setEncoding("utf8");
|
|
53
|
+
child.stderr?.on("data", (chunk) => {
|
|
54
|
+
this.stderrTail = (this.stderrTail + chunk).slice(-STDERR_TAIL_CHARS);
|
|
55
|
+
});
|
|
56
|
+
// A server that exits while co-maintainer writes would otherwise throw
|
|
57
|
+
// EPIPE out of the review.
|
|
58
|
+
child.stdin?.on("error", () => { });
|
|
59
|
+
child.on("error", (error) => this.end(new McpError(`could not be run: ${error.message}`)));
|
|
60
|
+
child.on("close", (code, signal) => this.end(new McpError(signal ? `exited on ${signal}` : `exited with code ${code ?? 1}`)));
|
|
61
|
+
}
|
|
62
|
+
/** Spawns the server, then `initialize`, `notifications/initialized` and
|
|
63
|
+
* `tools/list`, all within `timeoutMs`. A server that fails any of them is
|
|
64
|
+
* stopped before the error is thrown. */
|
|
65
|
+
static async start(config, options = {}) {
|
|
66
|
+
const timeoutMs = options.timeoutMs ?? MCP_STARTUP_MS;
|
|
67
|
+
const deadline = Date.now() + timeoutMs;
|
|
68
|
+
const left = () => Math.max(1, deadline - Date.now());
|
|
69
|
+
const spawn = options.spawn ??
|
|
70
|
+
((command, spawnOptions) => commandPipe(command, spawnOptions));
|
|
71
|
+
const client = new McpClient(spawn(config.command, {
|
|
72
|
+
args: config.args ?? [],
|
|
73
|
+
env: mcpEnv(config.env),
|
|
74
|
+
}));
|
|
75
|
+
try {
|
|
76
|
+
await client.request("initialize", {
|
|
77
|
+
protocolVersion: MCP_PROTOCOL_VERSION,
|
|
78
|
+
capabilities: {},
|
|
79
|
+
clientInfo: { name: "co-maintainer", version: VERSION },
|
|
80
|
+
}, left());
|
|
81
|
+
client.notify("notifications/initialized");
|
|
82
|
+
const tools = [];
|
|
83
|
+
let cursor;
|
|
84
|
+
for (let page = 0; page < MAX_LIST_PAGES; page++) {
|
|
85
|
+
const result = (await client.request("tools/list", cursor ? { cursor } : {}, left()));
|
|
86
|
+
const listed = Array.isArray(result?.tools)
|
|
87
|
+
? result.tools
|
|
88
|
+
: [];
|
|
89
|
+
for (const tool of listed) {
|
|
90
|
+
if (isRecord(tool) && typeof tool.name === "string") {
|
|
91
|
+
tools.push(tool);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
cursor =
|
|
95
|
+
typeof result?.nextCursor === "string" && result.nextCursor
|
|
96
|
+
? result.nextCursor
|
|
97
|
+
: undefined;
|
|
98
|
+
if (!cursor)
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
return { client, tools };
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
await client.stop();
|
|
105
|
+
throw error instanceof McpError
|
|
106
|
+
? error
|
|
107
|
+
: new McpError(error instanceof Error ? error.message : String(error));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/** What the server last printed on stderr, for a failed start's log line. */
|
|
111
|
+
stderr() {
|
|
112
|
+
return this.stderrTail.trim();
|
|
113
|
+
}
|
|
114
|
+
get alive() {
|
|
115
|
+
return this.ended === undefined;
|
|
116
|
+
}
|
|
117
|
+
/** One `tools/call`. Text content is kept, anything else is left out with
|
|
118
|
+
* a note. A timeout or an exit throws `McpError`. */
|
|
119
|
+
async call(name, args, timeoutMs) {
|
|
120
|
+
const result = (await this.request("tools/call", {
|
|
121
|
+
name,
|
|
122
|
+
arguments: args && typeof args === "object" && !Array.isArray(args) ? args : {},
|
|
123
|
+
}, timeoutMs));
|
|
124
|
+
const content = Array.isArray(result?.content)
|
|
125
|
+
? result.content
|
|
126
|
+
: [];
|
|
127
|
+
const texts = [];
|
|
128
|
+
let other = 0;
|
|
129
|
+
for (const item of content) {
|
|
130
|
+
if (isRecord(item) &&
|
|
131
|
+
item.type === "text" &&
|
|
132
|
+
typeof item.text === "string") {
|
|
133
|
+
texts.push(item.text);
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
other++;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (other > 0) {
|
|
140
|
+
texts.push(`[${other} ${other === 1 ? "part that is" : "parts that are"} not text left out]`);
|
|
141
|
+
}
|
|
142
|
+
return { text: texts.join("\n"), isError: result?.isError === true };
|
|
143
|
+
}
|
|
144
|
+
/** Closes stdin, which ends a well-behaved server, then SIGTERM, then
|
|
145
|
+
* SIGKILL. Never throws. */
|
|
146
|
+
async stop() {
|
|
147
|
+
const waitExit = (ms) => Promise.race([
|
|
148
|
+
this.exited.then(() => true),
|
|
149
|
+
new Promise((resolve) => {
|
|
150
|
+
const timer = setTimeout(() => resolve(false), ms);
|
|
151
|
+
timer.unref?.();
|
|
152
|
+
}),
|
|
153
|
+
]);
|
|
154
|
+
if (this.child.exitCode !== null || this.child.signalCode !== null) {
|
|
155
|
+
this.end(new McpError("stopped"));
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
try {
|
|
159
|
+
this.child.stdin?.end();
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
// already closed
|
|
163
|
+
}
|
|
164
|
+
if (!(await waitExit(STOP_GRACE_MS))) {
|
|
165
|
+
this.child.kill("SIGTERM");
|
|
166
|
+
if (!(await waitExit(STOP_GRACE_MS)))
|
|
167
|
+
this.child.kill("SIGKILL");
|
|
168
|
+
}
|
|
169
|
+
this.end(new McpError("stopped"));
|
|
170
|
+
}
|
|
171
|
+
end(error) {
|
|
172
|
+
if (!this.ended)
|
|
173
|
+
this.ended = error;
|
|
174
|
+
for (const [id, pending] of this.pending) {
|
|
175
|
+
clearTimeout(pending.timer);
|
|
176
|
+
this.pending.delete(id);
|
|
177
|
+
pending.reject(this.ended);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
send(message) {
|
|
181
|
+
if (this.ended)
|
|
182
|
+
return;
|
|
183
|
+
this.child.stdin?.write(`${JSON.stringify({ jsonrpc: "2.0", ...message })}\n`);
|
|
184
|
+
}
|
|
185
|
+
notify(method) {
|
|
186
|
+
this.send({ method });
|
|
187
|
+
}
|
|
188
|
+
request(method, params, timeoutMs) {
|
|
189
|
+
if (this.ended)
|
|
190
|
+
return Promise.reject(this.ended);
|
|
191
|
+
const id = this.nextId++;
|
|
192
|
+
return new Promise((resolve, reject) => {
|
|
193
|
+
const timer = setTimeout(() => {
|
|
194
|
+
this.pending.delete(id);
|
|
195
|
+
reject(new McpError(`did not answer ${method} within ${Number((timeoutMs / 1000).toFixed(1))} seconds`));
|
|
196
|
+
}, timeoutMs);
|
|
197
|
+
this.pending.set(id, { resolve, reject, timer });
|
|
198
|
+
this.send({ id, method, params });
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
read(chunk) {
|
|
202
|
+
this.buffer += chunk;
|
|
203
|
+
let newline = this.buffer.indexOf("\n");
|
|
204
|
+
while (newline !== -1) {
|
|
205
|
+
const line = this.buffer.slice(0, newline).trim();
|
|
206
|
+
this.buffer = this.buffer.slice(newline + 1);
|
|
207
|
+
if (line)
|
|
208
|
+
this.receive(line);
|
|
209
|
+
newline = this.buffer.indexOf("\n");
|
|
210
|
+
}
|
|
211
|
+
if (this.buffer.length > MAX_LINE_CHARS) {
|
|
212
|
+
this.buffer = "";
|
|
213
|
+
this.end(new McpError("sent a line longer than 16 MB"));
|
|
214
|
+
this.child.kill("SIGKILL");
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
receive(line) {
|
|
218
|
+
let message;
|
|
219
|
+
try {
|
|
220
|
+
const parsed = JSON.parse(line);
|
|
221
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
message = parsed;
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
// A server that logs to stdout: the line is not a message.
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if (typeof message.method === "string") {
|
|
231
|
+
// A request from the server. Only ping is answered with a result.
|
|
232
|
+
if (message.id === undefined || message.id === null)
|
|
233
|
+
return;
|
|
234
|
+
if (message.method === "ping") {
|
|
235
|
+
this.send({ id: message.id, result: {} });
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
this.send({
|
|
239
|
+
id: message.id,
|
|
240
|
+
error: { code: -32601, message: "Method not found" },
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
if (typeof message.id !== "number")
|
|
246
|
+
return;
|
|
247
|
+
const pending = this.pending.get(message.id);
|
|
248
|
+
if (!pending)
|
|
249
|
+
return;
|
|
250
|
+
clearTimeout(pending.timer);
|
|
251
|
+
this.pending.delete(message.id);
|
|
252
|
+
if (message.error && typeof message.error === "object") {
|
|
253
|
+
const error = message.error;
|
|
254
|
+
pending.reject(new McpRpcError(`answered with JSON-RPC error ${typeof error.code === "number" ? error.code : "?"}`));
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
pending.resolve(message.result);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** One stdio MCP server, as `config.json` stores it. */
|
|
2
|
+
export type McpServerConfig = {
|
|
3
|
+
command: string;
|
|
4
|
+
args?: string[];
|
|
5
|
+
/** The server's whole environment, with `PATH` and `HOME` added. */
|
|
6
|
+
env?: Record<string, string>;
|
|
7
|
+
/** The only tools offered to the model, by the server's own names. */
|
|
8
|
+
tools: string[];
|
|
9
|
+
/** Seconds one call may take. Unset means 20. */
|
|
10
|
+
timeoutSeconds?: number;
|
|
11
|
+
};
|
|
12
|
+
/** Seconds one tool call may take when the config sets none. */
|
|
13
|
+
export declare const DEFAULT_MCP_TIMEOUT_SECONDS = 20;
|
|
14
|
+
/** `CM_MCP_DISABLED=1` turns MCP off hard. The hosted cloud sets it from
|
|
15
|
+
* outside the container, so nothing in the user's data volume can turn it
|
|
16
|
+
* back on. */
|
|
17
|
+
export declare function mcpDisabled(): boolean;
|
|
18
|
+
export declare const MCP_DISABLED_MESSAGE = "MCP servers are turned off on this machine (CM_MCP_DISABLED=1).";
|
|
19
|
+
/** Thrown with a message that names what is wrong in the file. */
|
|
20
|
+
export declare class McpConfigError extends Error {
|
|
21
|
+
}
|
|
22
|
+
/** The servers in an `mcp.json` file's text. */
|
|
23
|
+
export declare function parseMcpConfig(text: string): Record<string, McpServerConfig>;
|
|
24
|
+
/** `tracker (get_issue, search_issues)`: what `set` prints. Never the
|
|
25
|
+
* command, its arguments or its env, which may hold secrets. */
|
|
26
|
+
export declare function mcpServersSummary(servers: Record<string, McpServerConfig>): string;
|