(server: McpServer, dbManager: McpDatabaseManager)
| 31 | } |
| 32 | |
| 33 | function registerTools(server: McpServer, dbManager: McpDatabaseManager): void { |
| 34 | for (const tool of MCP_TOOL_REGISTRY) { |
| 35 | const mcpName = `${MCP_TOOL_PREFIX}${tool.name}` |
| 36 | |
| 37 | if (tool.name === 'list_sessions') { |
| 38 | const zodShape = { |
| 39 | ...jsonSchemaToZod(tool.inputSchema.properties, tool.inputSchema.required), |
| 40 | format: FORMAT_PARAM, |
| 41 | } |
| 42 | |
| 43 | server.tool(mcpName, tool.description, zodShape, async (params) => { |
| 44 | const format = params.format as string | undefined |
| 45 | const context: SessionListContext = { |
| 46 | db: null as any, |
| 47 | sessionId: '', |
| 48 | listSessionIds: () => dbManager.listSessionIds(), |
| 49 | openDb: (id) => dbManager.open(id), |
| 50 | } |
| 51 | const toolParams = { ...params } as Record<string, unknown> |
| 52 | delete toolParams.format |
| 53 | const result = await tool.handler(toolParams, context) |
| 54 | const text = applyFormat(result.content, format, tool.name) |
| 55 | return { content: [{ type: 'text' as const, text }] } |
| 56 | }) |
| 57 | continue |
| 58 | } |
| 59 | |
| 60 | const zodShape = { |
| 61 | session_id: z.string().describe('Session ID'), |
| 62 | ...jsonSchemaToZod(tool.inputSchema.properties, tool.inputSchema.required), |
| 63 | format: FORMAT_PARAM, |
| 64 | } |
| 65 | |
| 66 | server.tool(mcpName, tool.description, zodShape, async (params) => { |
| 67 | const sessionId = params.session_id as string |
| 68 | const format = params.format as string | undefined |
| 69 | const db = dbManager.open(sessionId) |
| 70 | if (!db) { |
| 71 | return { |
| 72 | content: [{ type: 'text' as const, text: `Session not found: ${sessionId}` }], |
| 73 | isError: true, |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | const toolParams = { ...params } as Record<string, unknown> |
| 78 | delete toolParams.session_id |
| 79 | delete toolParams.format |
| 80 | |
| 81 | const result = await tool.handler(toolParams, { db, sessionId, dataProvider: new CoreDataProvider(db) }) |
| 82 | const text = applyFormat(result.content, format, tool.name) |
| 83 | return { content: [{ type: 'text' as const, text }] } |
| 84 | }) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | function registerResources(server: McpServer, dbManager: McpDatabaseManager): void { |
| 89 | server.resource('sessions-list', 'chatlab://sessions', { description: '所有已导入的聊天会话列表' }, async () => { |
no test coverage detected