UNPKG

bc_resource_mcp

Version:

MCP server for Baichuan resource

157 lines (156 loc) 5.96 kB
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { APIServerMCP } from "./helper.js"; import { createToolHandlers } from "../tools/registery.js"; import * as tools from "../tools/index.js"; import { z } from "zod"; import { MAIN_OVERFLOW_PROMPT } from "../prompts/index.js"; import { loadConfig } from "../utils/config.js"; // 注册字典工具 function registerDictionaryTool(server, getHandler, client) { const dictionaryHandler = getHandler(tools.TOOL_DICTIONARY); server.tool(dictionaryHandler.name, dictionaryHandler.getToolDescription().description, { subject: z .string() .optional() .describe(dictionaryHandler.getToolDescription().inputSchema.properties.subject .description), grade: z .string() .optional() .describe(dictionaryHandler.getToolDescription().inputSchema.properties.grade .description), volume: z .string() .optional() .describe(dictionaryHandler.getToolDescription().inputSchema.properties.volume .description), press: z .string() .optional() .describe(dictionaryHandler.getToolDescription().inputSchema.properties.press .description), }, async (args) => { const result = await client.runTool(dictionaryHandler.name, args); const content = result[0]; if (content.type === "text") { return { content: [{ type: "text", text: content.text }], }; } throw new Error("Unexpected result type from tool"); }); } // 注册图书列表工具 function registerListBooksTool(server, getHandler, client) { const listBooksHandler = getHandler(tools.TOOL_LIST_BOOKS); server.tool(listBooksHandler.name, listBooksHandler.getToolDescription().description, { subject: z .string() .optional() .describe(listBooksHandler.getToolDescription().inputSchema.properties.subject .description), grade: z .number() .optional() .describe(listBooksHandler.getToolDescription().inputSchema.properties.grade .description), volume: z .number() .optional() .describe(listBooksHandler.getToolDescription().inputSchema.properties.volume .description), press: z .number() .optional() .describe(listBooksHandler.getToolDescription().inputSchema.properties.press .description), }, async (args) => { const result = await client.runTool(listBooksHandler.name, args); const content = result[0]; if (content.type === "text") { return { content: [{ type: "text", text: content.text }], }; } throw new Error("Unexpected result type from tool"); }); } // 注册章节列表工具 function registerListChaptersTool(server, getHandler, client) { const listChaptersHandler = getHandler(tools.TOOL_LIST_CHAPTERS); server.tool(listChaptersHandler.name, listChaptersHandler.getToolDescription().description, { bookId: z .string() .describe(listChaptersHandler.getToolDescription().inputSchema.properties.bookId .description), }, async (args) => { const result = await client.runTool(listChaptersHandler.name, args); const content = result[0]; if (content.type === "text") { return { content: [{ type: "text", text: content.text }], }; } throw new Error("Unexpected result type from tool"); }); } // 注册获取资源工具 function registerFetchResourceTool(server, getHandler, client) { const fetchResourceHandler = getHandler(tools.TOOL_FETCH_RESOURCE); server.tool(fetchResourceHandler.name, fetchResourceHandler.getToolDescription().description, { id: z .string() .describe(fetchResourceHandler.getToolDescription().inputSchema.properties.id .description), }, async (args) => { const result = await client.runTool(fetchResourceHandler.name, args); const content = result[0]; if (content.type === "text") { return { content: [{ type: "text", text: content.text }], }; } throw new Error("Unexpected result type from tool"); }); } // 注册提示词 function registerPrompts(server) { server.prompt(MAIN_OVERFLOW_PROMPT.name, { user_input: z.string().describe("The content of user input") }, ({ user_input }) => { const promptText = MAIN_OVERFLOW_PROMPT.promptText.replace("{{user_input}}", user_input); return { messages: [ { role: "user", content: { type: "text", text: promptText, }, }, ], }; }); } export function createMcpServer(moduleId) { const client = new APIServerMCP({ moduleId: moduleId }); const toolHandlers = createToolHandlers(client.getAPIServer()); const getHandler = (name) => { const handler = toolHandlers.get(name); if (!handler) { throw new Error(`Tool ${name} not found`); } return handler; }; const config = loadConfig(); const server = new McpServer({ name: "bc_resource_mcp", version: config.version, }); // 注册所有工具 registerDictionaryTool(server, getHandler, client); registerListBooksTool(server, getHandler, client); registerListChaptersTool(server, getHandler, client); registerFetchResourceTool(server, getHandler, client); // 注册提示词 registerPrompts(server); return server; }