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.
@@ -0,0 +1,111 @@
1
+ /** A repository's MCP servers (issue 79): the file `set --mcp-config` reads,
2
+ * and the switch that turns MCP off on a machine.
3
+ *
4
+ * The file is read once and stored as `repos["owner/repo"].mcpServers` in
5
+ * `config.json`, so a review never reads a path that may have moved. `env`
6
+ * values are secrets for the server, so nothing here prints them. */
7
+ import { getEnv } from "../util/runtime.js";
8
+ /** Seconds one tool call may take when the config sets none. */
9
+ export const DEFAULT_MCP_TIMEOUT_SECONDS = 20;
10
+ /** `CM_MCP_DISABLED=1` turns MCP off hard. The hosted cloud sets it from
11
+ * outside the container, so nothing in the user's data volume can turn it
12
+ * back on. */
13
+ export function mcpDisabled() {
14
+ return getEnv("CM_MCP_DISABLED") === "1";
15
+ }
16
+ export const MCP_DISABLED_MESSAGE = "MCP servers are turned off on this machine (CM_MCP_DISABLED=1).";
17
+ /** A server name becomes part of the tool name `mcp-<server>-<tool>`, which
18
+ * every provider limits to letters, digits, dashes and underscores. A dash
19
+ * in the server name would make two names read the same, so it has none. */
20
+ const SERVER_NAME = /^[A-Za-z0-9_]{1,32}$/;
21
+ const SERVER_KEYS = new Set([
22
+ "command",
23
+ "args",
24
+ "env",
25
+ "tools",
26
+ "timeoutSeconds",
27
+ "type",
28
+ ]);
29
+ function isRecord(value) {
30
+ return typeof value === "object" && value !== null && !Array.isArray(value);
31
+ }
32
+ /** Thrown with a message that names what is wrong in the file. */
33
+ export class McpConfigError extends Error {
34
+ }
35
+ function fail(message) {
36
+ throw new McpConfigError(message);
37
+ }
38
+ function server(name, raw) {
39
+ if (!SERVER_NAME.test(name)) {
40
+ fail(`The server name "${name}" must be 1 to 32 letters, digits or underscores.`);
41
+ }
42
+ if (!isRecord(raw))
43
+ fail(`Server ${name} must be an object.`);
44
+ for (const key of Object.keys(raw)) {
45
+ if (!SERVER_KEYS.has(key)) {
46
+ fail(key === "url"
47
+ ? `Server ${name} has a url. Only stdio servers started with a command are supported.`
48
+ : `Server ${name} has an unknown key "${key}". Known keys: command, args, env, tools, timeoutSeconds.`);
49
+ }
50
+ }
51
+ if (raw.type !== undefined && raw.type !== "stdio") {
52
+ fail(`Server ${name} has type "${String(raw.type)}". Only stdio is supported.`);
53
+ }
54
+ if (typeof raw.command !== "string" || raw.command.trim() === "") {
55
+ fail(`Server ${name} needs a command.`);
56
+ }
57
+ const config = { command: raw.command, tools: [] };
58
+ if (raw.args !== undefined) {
59
+ if (!Array.isArray(raw.args) ||
60
+ raw.args.some((arg) => typeof arg !== "string")) {
61
+ fail(`Server ${name}: args must be a list of strings.`);
62
+ }
63
+ config.args = raw.args;
64
+ }
65
+ if (raw.env !== undefined) {
66
+ if (!isRecord(raw.env) ||
67
+ Object.values(raw.env).some((value) => typeof value !== "string")) {
68
+ fail(`Server ${name}: env must map names to string values.`);
69
+ }
70
+ config.env = { ...raw.env };
71
+ }
72
+ if (!Array.isArray(raw.tools) ||
73
+ raw.tools.length === 0 ||
74
+ raw.tools.some((tool) => typeof tool !== "string" || tool.trim() === "")) {
75
+ fail(`Server ${name} needs tools: a list of the read-only tool names to offer.`);
76
+ }
77
+ config.tools = [...new Set(raw.tools)];
78
+ if (raw.timeoutSeconds !== undefined) {
79
+ const seconds = raw.timeoutSeconds;
80
+ if (typeof seconds !== "number" || !(seconds > 0) || seconds > 600) {
81
+ fail(`Server ${name}: timeoutSeconds must be a number above 0 and at most 600.`);
82
+ }
83
+ config.timeoutSeconds = seconds;
84
+ }
85
+ return config;
86
+ }
87
+ /** The servers in an `mcp.json` file's text. */
88
+ export function parseMcpConfig(text) {
89
+ let parsed;
90
+ try {
91
+ parsed = JSON.parse(text);
92
+ }
93
+ catch {
94
+ fail("The MCP config is not valid JSON.");
95
+ }
96
+ if (!isRecord(parsed) || !isRecord(parsed.mcpServers)) {
97
+ fail('The MCP config needs an "mcpServers" object.');
98
+ }
99
+ const entries = Object.entries(parsed.mcpServers);
100
+ if (entries.length === 0) {
101
+ fail("The MCP config has no servers. Use --unset=mcp-config to remove them.");
102
+ }
103
+ return Object.fromEntries(entries.map(([name, raw]) => [name, server(name, raw)]));
104
+ }
105
+ /** `tracker (get_issue, search_issues)`: what `set` prints. Never the
106
+ * command, its arguments or its env, which may hold secrets. */
107
+ export function mcpServersSummary(servers) {
108
+ return Object.entries(servers)
109
+ .map(([name, config]) => `${name} (${config.tools.join(", ")})`)
110
+ .join(", ");
111
+ }
@@ -0,0 +1,39 @@
1
+ /** A repository's MCP servers as review tools (issue 79).
2
+ *
3
+ * The servers start once per review, before the first model call, and stop
4
+ * however the review ends. Each listed tool the server has and does not mark
5
+ * as writing is offered as `mcp-<server>-<tool>`. A server that does not
6
+ * start, or stops answering, gives `mcp_unavailable` and the review goes on
7
+ * without it.
8
+ *
9
+ * Every result is private. The session keeps each one, so the review can
10
+ * remove a copied passage from what it prints, posts and stores. A result is
11
+ * never logged. */
12
+ import type { ToolHandler } from "../ai/mermaid_loop.ts";
13
+ import type { CoverageWarning } from "../review/coverage.ts";
14
+ import type { Json } from "../types.ts";
15
+ import { type Spawn } from "./mcp_client.ts";
16
+ import { type McpServerConfig } from "./mcp_config.ts";
17
+ /** One review's MCP servers and the tools they give. */
18
+ export type McpSession = {
19
+ tools: ToolHandler[];
20
+ /** The servers that started, in config order, for the Metadata line. */
21
+ servers: string[];
22
+ warnings(): CoverageWarning[];
23
+ /** Every result the model received from an MCP tool in this review. */
24
+ privateResults(): string[];
25
+ /** Stops every server. Never throws, and a second call does nothing. */
26
+ stop(): Promise<void>;
27
+ };
28
+ export declare function emptyMcpSession(): McpSession;
29
+ export declare function mcpToolName(server: string, tool: string): string;
30
+ export declare function isMcpTool(name: string): boolean;
31
+ /** The tool's input schema in the provider's function format, or
32
+ * `undefined` when it cannot be sent. MCP requires an object schema. */
33
+ export declare function convertInputSchema(schema: unknown): Json | undefined;
34
+ export type McpSessionDeps = {
35
+ spawn?: Spawn;
36
+ startupMs?: number;
37
+ };
38
+ /** Starts every server at once and builds the tools. */
39
+ export declare function startMcpSession(servers: Record<string, McpServerConfig>, deps?: McpSessionDeps): Promise<McpSession>;
@@ -0,0 +1,150 @@
1
+ import { log } from "../util/log.js";
2
+ import { McpClient, McpError } from "./mcp_client.js";
3
+ import { DEFAULT_MCP_TIMEOUT_SECONDS, } from "./mcp_config.js";
4
+ export function emptyMcpSession() {
5
+ return {
6
+ tools: [],
7
+ servers: [],
8
+ warnings: () => [],
9
+ privateResults: () => [],
10
+ stop: async () => { },
11
+ };
12
+ }
13
+ /** Tool names every provider accepts. */
14
+ const TOOL_NAME = /^[A-Za-z0-9_-]{1,64}$/;
15
+ /** The longest server-written tool description the model is shown. */
16
+ const MAX_DESCRIPTION_CHARS = 1000;
17
+ export function mcpToolName(server, tool) {
18
+ return `mcp-${server}-${tool}`;
19
+ }
20
+ export function isMcpTool(name) {
21
+ return name.startsWith("mcp-");
22
+ }
23
+ /** The tool's input schema in the provider's function format, or
24
+ * `undefined` when it cannot be sent. MCP requires an object schema. */
25
+ export function convertInputSchema(schema) {
26
+ if (!schema || typeof schema !== "object" || Array.isArray(schema)) {
27
+ return undefined;
28
+ }
29
+ const { $schema: _ignored, ...rest } = schema;
30
+ if (rest.type !== "object")
31
+ return undefined;
32
+ if (rest.properties !== undefined &&
33
+ (typeof rest.properties !== "object" ||
34
+ rest.properties === null ||
35
+ Array.isArray(rest.properties))) {
36
+ return undefined;
37
+ }
38
+ try {
39
+ // A schema that cannot round-trip through JSON cannot be sent.
40
+ return JSON.parse(JSON.stringify(rest));
41
+ }
42
+ catch {
43
+ return undefined;
44
+ }
45
+ }
46
+ /** Starts every server at once and builds the tools. */
47
+ export async function startMcpSession(servers, deps = {}) {
48
+ const warnings = [];
49
+ const results = [];
50
+ const down = new Set();
51
+ const unavailable = (server, why) => {
52
+ if (down.has(server))
53
+ return;
54
+ down.add(server);
55
+ warnings.push({
56
+ code: "mcp_unavailable",
57
+ message: `The MCP server ${server} ${why}, so the review went on without it.`,
58
+ });
59
+ };
60
+ const settled = await Promise.all(Object.entries(servers).map(async ([name, config]) => {
61
+ try {
62
+ const { client, tools } = await McpClient.start(config, {
63
+ timeoutMs: deps.startupMs,
64
+ spawn: deps.spawn,
65
+ });
66
+ log("mcp", `${name} started · ${tools.length} tool(s) listed`);
67
+ return { name, config, client, tools };
68
+ }
69
+ catch (error) {
70
+ const reason = error instanceof McpError ? error.message : String(error);
71
+ log("mcp", `${name} did not start · ${reason}`);
72
+ unavailable(name, `did not start (${reason})`);
73
+ return undefined;
74
+ }
75
+ }));
76
+ const started = settled.filter((item) => !!item);
77
+ const tools = [];
78
+ for (const { name: server, config, client, tools: listed } of started) {
79
+ const timeoutMs = (config.timeoutSeconds ?? DEFAULT_MCP_TIMEOUT_SECONDS) * 1000;
80
+ for (const wanted of config.tools) {
81
+ const tool = listed.find((item) => item.name === wanted);
82
+ if (!tool) {
83
+ warnings.push({
84
+ code: "mcp_tool_missing",
85
+ message: `The MCP server ${server} has no tool named ${wanted}, so it was not offered.`,
86
+ });
87
+ continue;
88
+ }
89
+ if (tool.annotations?.readOnlyHint === false) {
90
+ warnings.push({
91
+ code: "mcp_tool_refused",
92
+ message: `The MCP server ${server} marks ${wanted} as not read-only, so it was not offered.`,
93
+ });
94
+ continue;
95
+ }
96
+ const name = mcpToolName(server, wanted);
97
+ const parameters = convertInputSchema(tool.inputSchema);
98
+ if (!TOOL_NAME.test(name) || !parameters) {
99
+ warnings.push({
100
+ code: "mcp_tool_refused",
101
+ message: `The tool ${wanted} of the MCP server ${server} cannot be sent to the model as ${name}, so it was not offered.`,
102
+ });
103
+ continue;
104
+ }
105
+ const own = typeof tool.description === "string" ? tool.description.trim() : "";
106
+ tools.push({
107
+ name,
108
+ external: true,
109
+ tool: {
110
+ type: "function",
111
+ function: {
112
+ name,
113
+ description: `Read-only tool ${wanted} of the repository's MCP server ${server}. Its results are private.${own ? ` ${own}` : ""}`.slice(0, MAX_DESCRIPTION_CHARS),
114
+ parameters,
115
+ },
116
+ },
117
+ run: async (args) => {
118
+ if (!client.alive || down.has(server)) {
119
+ return `The MCP server ${server} is not available. Answer without it.`;
120
+ }
121
+ try {
122
+ const result = await client.call(wanted, args, timeoutMs);
123
+ results.push(result.text);
124
+ return result.isError
125
+ ? `The tool answered with an error:\n${result.text}`
126
+ : result.text;
127
+ }
128
+ catch (error) {
129
+ if (error instanceof McpError) {
130
+ log("mcp", `${server} stopped answering · ${error.message}`);
131
+ unavailable(server, `stopped answering (${error.message})`);
132
+ void client.stop();
133
+ return `The MCP server ${server} stopped answering. Answer without it.`;
134
+ }
135
+ // A JSON-RPC error: the server is still up.
136
+ return `The MCP server ${server} could not run ${wanted}: ${error instanceof Error ? error.message : String(error)}.`;
137
+ }
138
+ },
139
+ });
140
+ }
141
+ }
142
+ let stopped;
143
+ return {
144
+ tools,
145
+ servers: started.map((item) => item.name),
146
+ warnings: () => [...warnings],
147
+ privateResults: () => [...results],
148
+ stop: () => (stopped ??= Promise.all(started.map((item) => item.client.stop())).then(() => { })),
149
+ };
150
+ }
@@ -17,6 +17,11 @@ export declare const SEARCH_MAX_MATCHES = 30;
17
17
  export declare const SEARCH_LINE_MAX_CHARS = 200;
18
18
  /** Time limit of a regex search. */
19
19
  export declare const SEARCH_REGEX_TIMEOUT_MS = 5000;
20
+ /** A literal search is fast, so its limit only stops a stuck one. */
21
+ export declare const SEARCH_LITERAL_TIMEOUT_MS = 15000;
22
+ /** Output kept from one git grep run. 30 matches need far less, the rest
23
+ * only feeds the per-file counts, so they say "at least" past this. */
24
+ export declare const SEARCH_MAX_OUTPUT_BYTES = 1000000;
20
25
  /** Default for `reviewSearchMaxContextLines`. */
21
26
  export declare const DEFAULT_SEARCH_MAX_CONTEXT_LINES = 3;
22
27
  /** The repository's most context lines around a match, or `null` for no
@@ -30,6 +35,9 @@ export type GrepResult = {
30
35
  stdout: string;
31
36
  stderr: string;
32
37
  timedOut: boolean;
38
+ /** The output passed `SEARCH_MAX_OUTPUT_BYTES` and the search was
39
+ * stopped there. */
40
+ cut?: boolean;
33
41
  };
34
42
  /** Runs `git` with `args` in `cwd`, killed after `timeoutMs` when set. */
35
43
  export type GrepRun = (args: string[], cwd: string, timeoutMs: number | null) => Promise<GrepResult>;
@@ -10,6 +10,11 @@ export const SEARCH_MAX_MATCHES = 30;
10
10
  export const SEARCH_LINE_MAX_CHARS = 200;
11
11
  /** Time limit of a regex search. */
12
12
  export const SEARCH_REGEX_TIMEOUT_MS = 5000;
13
+ /** A literal search is fast, so its limit only stops a stuck one. */
14
+ export const SEARCH_LITERAL_TIMEOUT_MS = 15_000;
15
+ /** Output kept from one git grep run. 30 matches need far less, the rest
16
+ * only feeds the per-file counts, so they say "at least" past this. */
17
+ export const SEARCH_MAX_OUTPUT_BYTES = 1_000_000;
13
18
  /** Default for `reviewSearchMaxContextLines`. */
14
19
  export const DEFAULT_SEARCH_MAX_CONTEXT_LINES = 3;
15
20
  /** Most files the `and N more matches` line names. */
@@ -69,15 +74,20 @@ export const runGit = async (args, cwd, timeoutMs) => {
69
74
  timedOut = true;
70
75
  proc.kill("SIGKILL");
71
76
  }, timeoutMs);
72
- const output = await proc.output();
77
+ const output = await proc.output(SEARCH_MAX_OUTPUT_BYTES);
73
78
  if (timer)
74
79
  clearTimeout(timer);
75
80
  const decoder = new TextDecoder();
81
+ let stdout = decoder.decode(output.stdout);
82
+ // A stopped run can end mid-record: keep only whole records.
83
+ if (output.cut)
84
+ stdout = stdout.slice(0, stdout.lastIndexOf("\n") + 1);
76
85
  return {
77
86
  code: output.code,
78
- stdout: decoder.decode(output.stdout),
87
+ stdout,
79
88
  stderr: decoder.decode(output.stderr),
80
89
  timedOut,
90
+ ...(output.cut ? { cut: true } : {}),
81
91
  };
82
92
  };
83
93
  /** `path\0line\0text` records, the `-z` output of `git grep -n`. */
@@ -154,9 +164,9 @@ export async function searchNonCode(root, args, options) {
154
164
  ? asked
155
165
  : Math.min(asked, options.maxContextLines);
156
166
  const run = options.run ?? runGit;
157
- const timeoutMs = regex
158
- ? (options.timeoutMs ?? SEARCH_REGEX_TIMEOUT_MS)
159
- : null;
167
+ // A literal search has a time limit too, only a longer one.
168
+ const timeoutMs = options.timeoutMs ??
169
+ (regex ? SEARCH_REGEX_TIMEOUT_MS : SEARCH_LITERAL_TIMEOUT_MS);
160
170
  // The match list comes without context, so a context line is never
161
171
  // mistaken for a match. `-z` marks both kinds the same way.
162
172
  const [matched, around] = await Promise.all([
@@ -220,9 +230,9 @@ export async function searchNonCode(root, args, options) {
220
230
  .slice(0, MAX_MORE_FILES)
221
231
  .map(([file, count]) => `${file} (${count})`);
222
232
  const unnamed = perFile.size - named.length;
223
- out.push(`and ${plural(rest.length, "more match", "more matches")}: ${named.join(", ")}${unnamed > 0 ? `, and ${plural(unnamed, "more file", "more files")}` : ""}`);
233
+ out.push(`and ${matched.cut ? "at least " : ""}${plural(rest.length, "more match", "more matches")}: ${named.join(", ")}${unnamed > 0 ? `, and ${plural(unnamed, "more file", "more files")}` : ""}`);
224
234
  }
225
- out.push(`${plural(matches.length, "match", "matches")} in ${plural(files.size, "file", "files")}`);
235
+ out.push(`${matched.cut ? "at least " : ""}${plural(matches.length, "match", "matches")} in ${plural(files.size, "file", "files")}`);
226
236
  return out.join("\n");
227
237
  }
228
238
  function noMatches(path) {
@@ -1,4 +1,5 @@
1
1
  import type { Stats } from "node:fs";
2
+ import { type ChildProcess } from "node:child_process";
2
3
  export declare function isWindows(): boolean;
3
4
  export type Platform = "windows" | "darwin" | "linux";
4
5
  /** The current OS in the vocabulary the config paths already used under Deno
@@ -83,11 +84,20 @@ export declare function commandOutput(command: string, options?: CommandOptions)
83
84
  /** `Deno.Command(...).spawn()` then write to stdin: used by the `gh` client
84
85
  * to pass a JSON body with `--input -`. */
85
86
  export declare function commandWithInput(command: string, options: CommandOptions, input: string): Promise<CommandOutput>;
87
+ /** A process whose three streams stay open, for a conversation over stdin
88
+ * and stdout such as an MCP server. `commandWithInput` writes once and closes
89
+ * stdin, which ends that conversation. The caller ends stdin and kills the
90
+ * process itself. */
91
+ export declare function commandPipe(command: string, options?: CommandOptions): ChildProcess;
86
92
  /** A spawned process that can be killed on timeout, mirroring
87
93
  * `Deno.Command(...).spawn()` + `proc.kill()`. */
88
94
  export declare function commandSpawn(command: string, options?: CommandOptions): {
89
95
  kill: (signal?: NodeJS.Signals) => void;
90
- output: () => Promise<CommandOutput>;
96
+ /** With `maxStdoutBytes`, the process is stopped once its output passes
97
+ * that size, and the result says `cut`. */
98
+ output: (maxStdoutBytes?: number) => Promise<CommandOutput & {
99
+ cut?: boolean;
100
+ }>;
91
101
  };
92
102
  /** Whether `pid` names a live process.
93
103
  *
@@ -153,19 +153,38 @@ function baseSpawnOptions(options) {
153
153
  shell: options.shell ?? false,
154
154
  };
155
155
  }
156
- function collect(child) {
156
+ function collect(child, maxStdoutBytes) {
157
157
  return new Promise((resolve, reject) => {
158
158
  const stdout = [];
159
159
  const stderr = [];
160
- child.stdout?.on("data", (chunk) => stdout.push(chunk));
160
+ let size = 0;
161
+ let cut = false;
162
+ child.stdout?.on("data", (chunk) => {
163
+ if (cut)
164
+ return;
165
+ stdout.push(chunk);
166
+ size += chunk.length;
167
+ // Past the limit the rest is not wanted: stop the process instead of
168
+ // holding all of its output.
169
+ if (maxStdoutBytes !== undefined && size > maxStdoutBytes) {
170
+ cut = true;
171
+ try {
172
+ child.kill("SIGKILL");
173
+ }
174
+ catch {
175
+ // already exited
176
+ }
177
+ }
178
+ });
161
179
  child.stderr?.on("data", (chunk) => stderr.push(chunk));
162
180
  child.on("error", reject);
163
181
  child.on("close", (code) => {
164
182
  resolve({
165
- success: code === 0,
166
- code: code ?? 1,
183
+ success: code === 0 || cut,
184
+ code: cut ? 0 : (code ?? 1),
167
185
  stdout: new Uint8Array(Buffer.concat(stdout)),
168
186
  stderr: new Uint8Array(Buffer.concat(stderr)),
187
+ ...(cut ? { cut: true } : {}),
169
188
  });
170
189
  });
171
190
  });
@@ -189,6 +208,16 @@ export function commandWithInput(command, options, input) {
189
208
  child.stdin?.end(input);
190
209
  return collect(child);
191
210
  }
211
+ /** A process whose three streams stay open, for a conversation over stdin
212
+ * and stdout such as an MCP server. `commandWithInput` writes once and closes
213
+ * stdin, which ends that conversation. The caller ends stdin and kills the
214
+ * process itself. */
215
+ export function commandPipe(command, options = {}) {
216
+ return spawn(command, options.args ?? [], {
217
+ ...baseSpawnOptions(options),
218
+ stdio: ["pipe", "pipe", "pipe"],
219
+ });
220
+ }
192
221
  /** A spawned process that can be killed on timeout, mirroring
193
222
  * `Deno.Command(...).spawn()` + `proc.kill()`. */
194
223
  export function commandSpawn(command, options = {}) {
@@ -205,7 +234,7 @@ export function commandSpawn(command, options = {}) {
205
234
  // already exited
206
235
  }
207
236
  },
208
- output: () => collect(child),
237
+ output: (maxStdoutBytes) => collect(child, maxStdoutBytes),
209
238
  };
210
239
  }
211
240
  /** Whether `pid` names a live process.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "co-maintainer",
3
- "version": "0.7.10-beta.2",
3
+ "version": "0.7.11-beta.2",
4
4
  "description": "An imitator of yours, A PR review tool like co-maintainer.",
5
5
  "keywords": [
6
6
  "ai",