mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-24 16:32:29 +00:00
* Tests: stabilize MCP config merge follow-ups * Commands: add /plugins chat command * Docs: add /plugins slash command guide
105 lines
3.2 KiB
TypeScript
105 lines
3.2 KiB
TypeScript
import { Command } from "commander";
|
|
import { parseConfigValue } from "../auto-reply/reply/config-value.js";
|
|
import {
|
|
listConfiguredMcpServers,
|
|
setConfiguredMcpServer,
|
|
unsetConfiguredMcpServer,
|
|
} from "../config/mcp-config.js";
|
|
import { defaultRuntime } from "../runtime.js";
|
|
|
|
function fail(message: string): never {
|
|
defaultRuntime.error(message);
|
|
defaultRuntime.exit(1);
|
|
throw new Error(message);
|
|
}
|
|
|
|
function printJson(value: unknown): void {
|
|
defaultRuntime.log(JSON.stringify(value, null, 2));
|
|
}
|
|
|
|
export function registerMcpCli(program: Command) {
|
|
const mcp = program.command("mcp").description("Manage OpenClaw MCP server config");
|
|
|
|
mcp
|
|
.command("list")
|
|
.description("List configured MCP servers")
|
|
.option("--json", "Print JSON")
|
|
.action(async (opts: { json?: boolean }) => {
|
|
const loaded = await listConfiguredMcpServers();
|
|
if (!loaded.ok) {
|
|
fail(loaded.error);
|
|
}
|
|
if (opts.json) {
|
|
printJson(loaded.mcpServers);
|
|
return;
|
|
}
|
|
const names = Object.keys(loaded.mcpServers).toSorted();
|
|
if (names.length === 0) {
|
|
defaultRuntime.log(`No MCP servers configured in ${loaded.path}.`);
|
|
return;
|
|
}
|
|
defaultRuntime.log(`MCP servers (${loaded.path}):`);
|
|
for (const name of names) {
|
|
defaultRuntime.log(`- ${name}`);
|
|
}
|
|
});
|
|
|
|
mcp
|
|
.command("show")
|
|
.description("Show one configured MCP server or the full MCP config")
|
|
.argument("[name]", "MCP server name")
|
|
.option("--json", "Print JSON")
|
|
.action(async (name: string | undefined, opts: { json?: boolean }) => {
|
|
const loaded = await listConfiguredMcpServers();
|
|
if (!loaded.ok) {
|
|
fail(loaded.error);
|
|
}
|
|
const value = name ? loaded.mcpServers[name] : loaded.mcpServers;
|
|
if (name && !value) {
|
|
fail(`No MCP server named "${name}" in ${loaded.path}.`);
|
|
}
|
|
if (opts.json) {
|
|
printJson(value ?? {});
|
|
return;
|
|
}
|
|
if (name) {
|
|
defaultRuntime.log(`MCP server "${name}" (${loaded.path}):`);
|
|
} else {
|
|
defaultRuntime.log(`MCP servers (${loaded.path}):`);
|
|
}
|
|
printJson(value ?? {});
|
|
});
|
|
|
|
mcp
|
|
.command("set")
|
|
.description("Set one configured MCP server from a JSON object")
|
|
.argument("<name>", "MCP server name")
|
|
.argument("<value>", 'JSON object, for example {"command":"uvx","args":["context7-mcp"]}')
|
|
.action(async (name: string, rawValue: string) => {
|
|
const parsed = parseConfigValue(rawValue);
|
|
if (parsed.error) {
|
|
fail(parsed.error);
|
|
}
|
|
const result = await setConfiguredMcpServer({ name, server: parsed.value });
|
|
if (!result.ok) {
|
|
fail(result.error);
|
|
}
|
|
defaultRuntime.log(`Saved MCP server "${name}" to ${result.path}.`);
|
|
});
|
|
|
|
mcp
|
|
.command("unset")
|
|
.description("Remove one configured MCP server")
|
|
.argument("<name>", "MCP server name")
|
|
.action(async (name: string) => {
|
|
const result = await unsetConfiguredMcpServer({ name });
|
|
if (!result.ok) {
|
|
fail(result.error);
|
|
}
|
|
if (!result.removed) {
|
|
fail(`No MCP server named "${name}" in ${result.path}.`);
|
|
}
|
|
defaultRuntime.log(`Removed MCP server "${name}" from ${result.path}.`);
|
|
});
|
|
}
|