Files
cobble/src/harness/mcp/mcp-harness-tool.ts
T
tbenandClaude Opus 5.5 c6ec7d77a2 Add MCP servers and custom tools for bots
Two new settings pages, MCPs and Tools. Super admins define them; super
admins and admins attach them to bots, to workspaces (every bot there),
or to every bot. Bots get them in chat replies and tasks.

- Custom tools: a JSON-schema of arguments plus an HTTP request, a script
  on the bot's computer, or a prompt, templated with {{args.x}} and
  {{env.KEY}}, secrets redacted from results.
- MCP servers: remote (URL and headers) or stdio commands run on the host
  with a setup command and an isolated env, in a per-process pool. Tools
  are listed into a cache and connect on first call.
- Add, edit, access and try open in modals.
- Markdown, JSON and command fields use twui's AceEditor (CodeEditor),
  its scripts loaded on demand.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-19 15:01:39 +01:00

60 lines
2.0 KiB
TypeScript

import type { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { AppData } from "@/src/data/app-data";
import type { HarnessTool } from "../types";
import mcpResultToText, { type McpImage } from "./mcp-result-to-text";
type Params = {
/**
* What the model calls it. Defaults to the server's name for it.
*/
name?: string;
/**
* The tool as the server lists it.
*/
tool: { name: string; description?: string; input_schema: Record<string, unknown> };
/**
* The connected client, or how to get it on the first call.
*/
client: Client | (() => Promise<Client>);
describeImage?: (image: McpImage) => Promise<string>;
redact?: (text: string) => string;
};
/**
* One MCP server tool as a harness tool. Arguments go to the server as the
* model wrote them (the server checks them against its own schema);
* results come back as text via `mcpResultToText`, redacted.
*/
export default function mcpHarnessTool({
name,
tool,
client,
describeImage,
redact = (text) => text,
}: Params): HarnessTool {
return {
name: name || tool.name,
description: tool.description || tool.name,
parameters: tool.input_schema,
run: async ({ args, signal }) => {
try {
const connected = typeof client == "function" ? await client() : client;
return redact(
await mcpResultToText({
result: await connected.callTool({ name: tool.name, arguments: args }, undefined, {
signal,
timeout: AppData.McpToolTimeoutMs,
resetTimeoutOnProgress: true,
}),
describeImage,
}),
);
} catch (error: unknown) {
if (error instanceof Error) error.message = redact(error.message);
throw error;
}
},
};
}