Give bots tools and MCP servers per bot and workspace, with status and tests
- Bots and workspaces get tools and MCP servers from their settings pages; bots also from chips under the chat composer, removable there - "Every bot" now gives one to every bot now and every new bot; each can still drop it (attach-every-bot-extensions.ts moves old installs over) - Status lines: MCP servers record whether they connect, tools whether their last run worked - Test connection in the MCP form and a Test panel in the tool form check the form as it is, without saving - Hide Google sign-in on the login form Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -1,12 +1,9 @@
|
||||
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
||||
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
||||
import type { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
||||
import type { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
||||
import { AppData } from "@/src/data/app-data";
|
||||
import { SiteData } from "@/src/data/site-data";
|
||||
import type { McpServerRecord } from "@/src/types";
|
||||
import grabMcpDir from "./grab-mcp-dir";
|
||||
import grabMcpStdioEnv from "./grab-mcp-stdio-env";
|
||||
import hostShell from "./host-shell";
|
||||
import runMcpSetup from "./run-mcp-setup";
|
||||
import startStdioMcpClient from "./start-stdio-mcp-client";
|
||||
|
||||
type Entry = {
|
||||
/**
|
||||
@@ -31,11 +28,9 @@ type Params = {
|
||||
};
|
||||
|
||||
/**
|
||||
* A connected client for a stdio MCP server, started on first use (after
|
||||
* its setup, `runMcpSetup`) with the host's shell in its directory, and
|
||||
* kept running for later calls until it's idle for `AppData.McpStdioIdleMs`,
|
||||
* its configuration changes, or it exits. Its stderr's last lines explain
|
||||
* a failed start.
|
||||
* A connected client for a stdio MCP server, started on first use
|
||||
* (`startStdioMcpClient`) and kept running for later calls until it's idle for `AppData.McpStdioIdleMs`,
|
||||
* its configuration changes, or it exits.
|
||||
*/
|
||||
export default async function grabStdioMcpClient({ server, envs }: Params): Promise<Client> {
|
||||
const env = grabMcpStdioEnv({ server, envs });
|
||||
@@ -77,25 +72,10 @@ export function stopStdioMcpServer({ server_id }: { server_id: number }) {
|
||||
}
|
||||
|
||||
async function start({ server, env, entry }: { server: McpServerRecord; env: Record<string, string>; entry: Entry }) {
|
||||
await runMcpSetup({ server, env });
|
||||
|
||||
const transport = new StdioClientTransport({
|
||||
command: hostShell(),
|
||||
args: ["-c", server.command],
|
||||
cwd: await grabMcpDir({ server_id: server.id }),
|
||||
env,
|
||||
stderr: "pipe",
|
||||
});
|
||||
let stderr = "";
|
||||
|
||||
transport.stderr?.on("data", (chunk: Buffer) => {
|
||||
stderr = `${stderr}${chunk.toString()}`.slice(-4000);
|
||||
});
|
||||
const { client, transport } = await startStdioMcpClient({ server, env });
|
||||
|
||||
entry.transport = transport;
|
||||
|
||||
const client = new Client({ name: SiteData["SiteSlug"], version: "1.0.0" });
|
||||
|
||||
client.onclose = () => {
|
||||
if (pool.get(server.id) == entry) {
|
||||
pool.delete(server.id);
|
||||
@@ -103,17 +83,6 @@ async function start({ server, env, entry }: { server: McpServerRecord; env: Rec
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
await client.connect(transport, { timeout: AppData.McpConnectTimeoutMs * 4 });
|
||||
} catch (error) {
|
||||
await client.close().catch(() => {});
|
||||
throw new Error(
|
||||
`Couldn't start ${server.name}: ${error instanceof Error ? error.message : String(error)}${
|
||||
stderr.trim() ? `\n${stderr.trim().slice(-2000)}` : ""
|
||||
}`,
|
||||
);
|
||||
}
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
||||
import recordMcpServerConnection from "@/src/functions/backend/db/extensions/record-mcp-server-connection";
|
||||
import type { McpServerRecord } from "@/src/types";
|
||||
import fillTemplate from "@/src/utils/fill-template";
|
||||
import connectMcpServer from "./connect-mcp-server";
|
||||
@@ -17,13 +18,39 @@ type Params = {
|
||||
* How one run reaches an MCP server, without connecting yet: `client()`
|
||||
* connects on the first call and is reused after. A remote server gets
|
||||
* its own connection for the run (closed by `close`); a stdio server is
|
||||
* shared through the process's pool and keeps running.
|
||||
* shared through the process's pool and keeps running. Whether it
|
||||
* connected is saved on the server when that changes
|
||||
* (`recordMcpServerConnection`).
|
||||
*/
|
||||
export default function openMcpServer({ server, envs, signal }: Params) {
|
||||
let remote: Promise<Client> | undefined;
|
||||
|
||||
let recorded = false;
|
||||
|
||||
/**
|
||||
* Once per run: an error every time (its message may change), a
|
||||
* success only when the server was failing.
|
||||
*/
|
||||
const record = <T>(connecting: Promise<T>) =>
|
||||
connecting.then(
|
||||
async (client) => {
|
||||
if (!recorded && server.status != "ok") await recordMcpServerConnection({ mcp_server_id: server.id });
|
||||
recorded = true;
|
||||
return client;
|
||||
},
|
||||
async (error: unknown) => {
|
||||
if (!signal?.aborted) {
|
||||
await recordMcpServerConnection({
|
||||
mcp_server_id: server.id,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
}
|
||||
throw error;
|
||||
},
|
||||
);
|
||||
|
||||
const client = () => {
|
||||
if (server.transport == "stdio") return grabStdioMcpClient({ server, envs });
|
||||
if (server.transport == "stdio") return record(grabStdioMcpClient({ server, envs }));
|
||||
|
||||
remote ||= connectMcpServer({
|
||||
url: fillTemplate({ template: server.url, envs }),
|
||||
@@ -36,7 +63,7 @@ export default function openMcpServer({ server, envs, signal }: Params) {
|
||||
throw error;
|
||||
});
|
||||
|
||||
return remote;
|
||||
return record(remote);
|
||||
};
|
||||
|
||||
const close = async () => {
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
||||
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
||||
import { AppData } from "@/src/data/app-data";
|
||||
import { SiteData } from "@/src/data/site-data";
|
||||
import type { McpServerRecord } from "@/src/types";
|
||||
import grabMcpDir from "./grab-mcp-dir";
|
||||
import hostShell from "./host-shell";
|
||||
import runMcpSetup from "./run-mcp-setup";
|
||||
|
||||
type Params = {
|
||||
server: McpServerRecord;
|
||||
/**
|
||||
* Its environment (`grabMcpStdioEnv`).
|
||||
*/
|
||||
env: Record<string, string>;
|
||||
};
|
||||
|
||||
/**
|
||||
* Starts a stdio MCP server after its setup (`runMcpSetup`), with the
|
||||
* host's shell in its directory, and connects to it. A failed start
|
||||
* throws with the last lines it wrote to stderr. Closing the client stops
|
||||
* it.
|
||||
*/
|
||||
export default async function startStdioMcpClient({ server, env }: Params) {
|
||||
await runMcpSetup({ server, env });
|
||||
|
||||
const transport = new StdioClientTransport({
|
||||
command: hostShell(),
|
||||
args: ["-c", server.command],
|
||||
cwd: await grabMcpDir({ server_id: server.id }),
|
||||
env,
|
||||
stderr: "pipe",
|
||||
});
|
||||
let stderr = "";
|
||||
|
||||
transport.stderr?.on("data", (chunk: Buffer) => {
|
||||
stderr = `${stderr}${chunk.toString()}`.slice(-4000);
|
||||
});
|
||||
|
||||
const client = new Client({ name: SiteData["SiteSlug"], version: "1.0.0" });
|
||||
|
||||
try {
|
||||
await client.connect(transport, { timeout: AppData.McpConnectTimeoutMs * 4 });
|
||||
} catch (error) {
|
||||
await client.close().catch(() => {});
|
||||
throw new Error(
|
||||
`Couldn't start ${server.name}: ${error instanceof Error ? error.message : String(error)}${
|
||||
stderr.trim() ? `\n${stderr.trim().slice(-2000)}` : ""
|
||||
}`,
|
||||
);
|
||||
}
|
||||
|
||||
return { client, transport };
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import type { McpServerRecord } from "@/src/types";
|
||||
import fillTemplate from "@/src/utils/fill-template";
|
||||
import connectMcpServer from "./connect-mcp-server";
|
||||
import grabMcpStdioEnv from "./grab-mcp-stdio-env";
|
||||
import listMcpTools from "./list-mcp-tools";
|
||||
import startStdioMcpClient from "./start-stdio-mcp-client";
|
||||
|
||||
type Params = {
|
||||
/**
|
||||
* As it would be saved; it may not be yet.
|
||||
*/
|
||||
server: McpServerRecord;
|
||||
envs: { key: string; value: string }[];
|
||||
};
|
||||
|
||||
/**
|
||||
* Connects to an MCP server as configured, lists its tools and
|
||||
* disconnects, outside the stdio pool, so a form can check a server
|
||||
* before it's saved. A stdio one runs its setup first, in its own
|
||||
* directory (a new server's in `mcp/0`). Throws when it can't connect.
|
||||
*/
|
||||
export default async function testMcpServer({ server, envs }: Params) {
|
||||
const client =
|
||||
server.transport == "stdio"
|
||||
? (await startStdioMcpClient({ server, env: grabMcpStdioEnv({ server, envs }) })).client
|
||||
: await connectMcpServer({
|
||||
url: fillTemplate({ template: server.url, envs }),
|
||||
headers: Object.fromEntries(
|
||||
Object.entries(server.headers).map(([key, value]) => [key, fillTemplate({ template: value, envs })]),
|
||||
),
|
||||
});
|
||||
|
||||
try {
|
||||
return await listMcpTools({ client });
|
||||
} finally {
|
||||
await client.close().catch(() => {});
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import recordCustomToolRun from "@/src/functions/backend/db/extensions/record-custom-tool-run";
|
||||
import type { CustomToolRecord } from "@/src/types";
|
||||
import redactSecrets from "@/src/utils/redact-secrets";
|
||||
import type { HarnessTool } from "../../types";
|
||||
@@ -19,12 +20,18 @@ type Params = {
|
||||
computer_id?: number | string | null;
|
||||
bot_id?: number | string;
|
||||
workspace_id?: number | string | null;
|
||||
/**
|
||||
* Saves how each run went on the tool. Off for a draft being tried.
|
||||
*/
|
||||
record?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* A custom tool as a harness tool: its name, description and JSON schema
|
||||
* as the model sees them, and `run` dispatching on its kind (an HTTP
|
||||
* request, a script on the computer, or a prompt). Null for a script tool
|
||||
* request, a script on the computer, or a prompt). How each run went is
|
||||
* saved on the tool (`recordCustomToolRun`); bad arguments and cancelled
|
||||
* runs aren't the tool's fault and don't count. Null for a script tool
|
||||
* when the run has no computer.
|
||||
*/
|
||||
export default function buildCustomTool({
|
||||
@@ -33,6 +40,7 @@ export default function buildCustomTool({
|
||||
computer_id,
|
||||
bot_id,
|
||||
workspace_id,
|
||||
record = true,
|
||||
}: Params): HarnessTool<Record<string, unknown>> | null {
|
||||
if (tool.kind == "script" && !computer_id) return null;
|
||||
|
||||
@@ -58,11 +66,22 @@ export default function buildCustomTool({
|
||||
: tool.description,
|
||||
parameters: tool.parameters,
|
||||
run: async ({ args, signal }) => {
|
||||
checkRequiredArgs({ parameters: tool.parameters, args });
|
||||
|
||||
try {
|
||||
checkRequiredArgs({ parameters: tool.parameters, args });
|
||||
return redact(await call({ args, signal }));
|
||||
const result = redact(await call({ args, signal }));
|
||||
if (record) await recordCustomToolRun({ custom_tool_id: tool.id });
|
||||
return result;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) error.message = redact(error.message);
|
||||
|
||||
if (record && !signal.aborted) {
|
||||
await recordCustomToolRun({
|
||||
custom_tool_id: tool.id,
|
||||
error: error instanceof Error ? error.message : redact(String(error)),
|
||||
});
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user