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]>
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import type { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
||||
import type { McpServerRecord } from "@/src/types";
|
||||
import fillTemplate from "@/src/utils/fill-template";
|
||||
import connectMcpServer from "./connect-mcp-server";
|
||||
import grabStdioMcpClient from "./mcp-stdio-pool";
|
||||
|
||||
type Params = {
|
||||
server: McpServerRecord;
|
||||
/**
|
||||
* What its templates (`{{env.KEY}}`) read.
|
||||
*/
|
||||
envs: { key: string; value: string }[];
|
||||
signal?: AbortSignal;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
export default function openMcpServer({ server, envs, signal }: Params) {
|
||||
let remote: Promise<Client> | undefined;
|
||||
|
||||
const client = () => {
|
||||
if (server.transport == "stdio") return grabStdioMcpClient({ server, envs });
|
||||
|
||||
remote ||= connectMcpServer({
|
||||
url: fillTemplate({ template: server.url, envs }),
|
||||
headers: Object.fromEntries(
|
||||
Object.entries(server.headers).map(([key, value]) => [key, fillTemplate({ template: value, envs })]),
|
||||
),
|
||||
signal,
|
||||
}).catch((error) => {
|
||||
remote = undefined;
|
||||
throw error;
|
||||
});
|
||||
|
||||
return remote;
|
||||
};
|
||||
|
||||
const close = async () => {
|
||||
if (!remote) return;
|
||||
await (await remote.catch(() => null))?.close().catch(() => {});
|
||||
};
|
||||
|
||||
return { client, close };
|
||||
}
|
||||
Reference in New Issue
Block a user