mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 22:40:42 +00:00
49 lines
1.7 KiB
TypeScript
49 lines
1.7 KiB
TypeScript
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
import type { AnyAgentTool } from "../agents/tools/common.js";
|
|
import { routeLogsToStderr } from "../logging/console.js";
|
|
import { VERSION } from "../version.js";
|
|
import { createPluginToolsMcpHandlers } from "./plugin-tools-handlers.js";
|
|
|
|
export function createToolsMcpServer(params: { name: string; tools: AnyAgentTool[] }): Server {
|
|
const handlers = createPluginToolsMcpHandlers(params.tools);
|
|
const server = new Server(
|
|
{ name: params.name, version: VERSION },
|
|
{ capabilities: { tools: {} } },
|
|
);
|
|
|
|
server.setRequestHandler(ListToolsRequestSchema, handlers.listTools);
|
|
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
return await handlers.callTool(request.params);
|
|
});
|
|
|
|
return server;
|
|
}
|
|
|
|
export async function connectToolsMcpServerToStdio(server: Server): Promise<void> {
|
|
// MCP stdio requires stdout to stay protocol-only.
|
|
routeLogsToStderr();
|
|
|
|
const transport = new StdioServerTransport();
|
|
let shuttingDown = false;
|
|
const shutdown = () => {
|
|
if (shuttingDown) {
|
|
return;
|
|
}
|
|
shuttingDown = true;
|
|
process.stdin.off("end", shutdown);
|
|
process.stdin.off("close", shutdown);
|
|
process.off("SIGINT", shutdown);
|
|
process.off("SIGTERM", shutdown);
|
|
void server.close();
|
|
};
|
|
|
|
process.stdin.once("end", shutdown);
|
|
process.stdin.once("close", shutdown);
|
|
process.once("SIGINT", shutdown);
|
|
process.once("SIGTERM", shutdown);
|
|
|
|
await server.connect(transport);
|
|
}
|