mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-10-25 17:54:34 +08:00
refactor(mcp): trim exposed tool registry
This commit is contained in:
1 parent
5db4b3bf9d
commit
3a2d4e753a
4 files changed
+25
-26
No files matched your search
@@ -21,7 +21,6 @@ import { getServerAiLogger } from './logger'
|
|||||||
const DEFAULT_MAX_TOOL_RESULT_TOKENS = 8000
|
const DEFAULT_MAX_TOOL_RESULT_TOKENS = 8000
|
||||||
|
|
||||||
const TOOL_TRUNCATION_STRATEGY: Record<string, TruncationStrategy> = {
|
const TOOL_TRUNCATION_STRATEGY: Record<string, TruncationStrategy> = {
|
||||||
search_keyword: 'keep_first',
|
|
||||||
search_messages: 'keep_first',
|
search_messages: 'keep_first',
|
||||||
deep_search_messages: 'keep_first',
|
deep_search_messages: 'keep_first',
|
||||||
get_recent_messages: 'keep_last',
|
get_recent_messages: 'keep_last',
|
||||||
|
|||||||
@@ -17,10 +17,7 @@ import { formatToolResultAsText } from './format'
|
|||||||
|
|
||||||
const MCP_TOOL_PREFIX = 'chatlab_'
|
const MCP_TOOL_PREFIX = 'chatlab_'
|
||||||
|
|
||||||
const FORMAT_PARAM = z
|
const FORMAT_PARAM = z.enum(['text', 'json']).optional().describe('text (default) or json')
|
||||||
.enum(['text', 'json'])
|
|
||||||
.optional()
|
|
||||||
.describe('Output format: "text" for token-efficient plain text (default), "json" for structured JSON')
|
|
||||||
|
|
||||||
/** Tools where JSON is a better default (raw SQL results, schema) */
|
/** Tools where JSON is a better default (raw SQL results, schema) */
|
||||||
const JSON_DEFAULT_TOOLS = new Set(['execute_sql', 'get_schema'])
|
const JSON_DEFAULT_TOOLS = new Set(['execute_sql', 'get_schema'])
|
||||||
@@ -60,9 +57,8 @@ function registerTools(server: McpServer, dbManager: McpDatabaseManager): void {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
const sessionsToolMcpName = `${MCP_TOOL_PREFIX}list_sessions`
|
|
||||||
const zodShape = {
|
const zodShape = {
|
||||||
session_id: z.string().describe(`Session ID (use ${sessionsToolMcpName} to get available sessions)`),
|
session_id: z.string().describe('Session ID'),
|
||||||
...jsonSchemaToZod(tool.inputSchema.properties, tool.inputSchema.required),
|
...jsonSchemaToZod(tool.inputSchema.properties, tool.inputSchema.required),
|
||||||
format: FORMAT_PARAM,
|
format: FORMAT_PARAM,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ export { CoreDataProvider } from './providers/core-data-provider'
|
|||||||
// === Tool Definitions ===
|
// === Tool Definitions ===
|
||||||
export { memberStatsTool } from './definitions/member-stats'
|
export { memberStatsTool } from './definitions/member-stats'
|
||||||
export { timeStatsTool } from './definitions/time-stats'
|
export { timeStatsTool } from './definitions/time-stats'
|
||||||
export { searchTool } from './definitions/search'
|
|
||||||
export { recentMessagesTool } from './definitions/recent-messages'
|
export { recentMessagesTool } from './definitions/recent-messages'
|
||||||
export { sqlQueryTool, schemaTool } from './definitions/sql-query'
|
export { sqlQueryTool, schemaTool } from './definitions/sql-query'
|
||||||
export { chatOverviewTool } from './definitions/chat-overview'
|
export { chatOverviewTool } from './definitions/chat-overview'
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
/**
|
/**
|
||||||
* 工具注册表
|
* Tool registries
|
||||||
*
|
*
|
||||||
* AGENT_TOOL_REGISTRY: Agent 共用(Server Agent / Electron Agent),
|
* AGENT_TOOL_REGISTRY: full toolset for Server Agent / Electron Agent
|
||||||
* 与 Electron TOOL_REGISTRY 保持一致的完整工具集。
|
* (core + analysis + raw SQL + declarative SQL tools).
|
||||||
* MCP_TOOL_REGISTRY: MCP Server 使用,包含 Agent 全量工具 + MCP 特有的会话发现工具。
|
* MCP_TOOL_REGISTRY: slim toolset for MCP Server — core + analysis + raw SQL
|
||||||
|
* plus MCP-specific session-discovery tools. Omits declarative SQL tools
|
||||||
|
* to keep tool-schema token cost low for external AI agents.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import type { ToolDefinition } from './types'
|
import type { ToolDefinition } from './types'
|
||||||
|
|
||||||
import { searchTool } from './definitions/search'
|
|
||||||
import { sqlQueryTool, schemaTool } from './definitions/sql-query'
|
import { sqlQueryTool, schemaTool } from './definitions/sql-query'
|
||||||
import { sessionInfoTool } from './definitions/session-info'
|
import { sessionInfoTool } from './definitions/session-info'
|
||||||
import { sessionsListTool } from './definitions/sessions'
|
import { sessionsListTool } from './definitions/sessions'
|
||||||
@@ -30,12 +31,10 @@ import { keywordFrequencyTool } from './definitions/keyword-frequency'
|
|||||||
import { SQL_TOOL_DEFS, createAllSqlToolDefinitions } from './sql'
|
import { SQL_TOOL_DEFS, createAllSqlToolDefinitions } from './sql'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Agent 共用注册表
|
* Core + analysis + raw SQL — shared between Agent and MCP.
|
||||||
*
|
* New non-SQL tools added here will automatically appear in both registries.
|
||||||
* 与 Electron TOOL_REGISTRY 完全对齐的工具集,在"已选定会话"上下文中使用。
|
|
||||||
* 不包含 MCP 特有的会话发现工具(list_sessions、get_session_info、search_keyword)。
|
|
||||||
*/
|
*/
|
||||||
export const AGENT_TOOL_REGISTRY: ToolDefinition[] = [
|
const SHARED_TOOLS: ToolDefinition[] = [
|
||||||
// Core
|
// Core
|
||||||
chatOverviewTool,
|
chatOverviewTool,
|
||||||
searchMessagesTool,
|
searchMessagesTool,
|
||||||
@@ -55,25 +54,30 @@ export const AGENT_TOOL_REGISTRY: ToolDefinition[] = [
|
|||||||
responseTimeAnalysisTool,
|
responseTimeAnalysisTool,
|
||||||
keywordFrequencyTool,
|
keywordFrequencyTool,
|
||||||
|
|
||||||
// SQL
|
// Raw SQL
|
||||||
sqlQueryTool,
|
sqlQueryTool,
|
||||||
...createAllSqlToolDefinitions(SQL_TOOL_DEFS),
|
|
||||||
]
|
]
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MCP Server 注册表
|
* Agent full toolset (Server Agent / Electron Agent).
|
||||||
|
* Includes declarative SQL convenience tools on top of the shared set.
|
||||||
|
*/
|
||||||
|
export const AGENT_TOOL_REGISTRY: ToolDefinition[] = [...SHARED_TOOLS, ...createAllSqlToolDefinitions(SQL_TOOL_DEFS)]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MCP Server toolset — slim registry optimised for external AI agents.
|
||||||
*
|
*
|
||||||
* 包含 MCP 特有工具(会话发现、schema)以及完整的 Agent 工具集。
|
* Includes MCP-specific session-discovery tools + shared core/analysis tools.
|
||||||
* MCP 层自动为非 list_sessions 工具注入 session_id 参数。
|
* Omits declarative SQL tools to reduce tool-schema token overhead (~40% saving).
|
||||||
|
* LLMs can use execute_sql + get_schema for any custom query.
|
||||||
*/
|
*/
|
||||||
export const MCP_TOOL_REGISTRY: ToolDefinition[] = [
|
export const MCP_TOOL_REGISTRY: ToolDefinition[] = [
|
||||||
// MCP-specific: session discovery & schema
|
// MCP-specific: session discovery & schema
|
||||||
sessionsListTool,
|
sessionsListTool,
|
||||||
sessionInfoTool,
|
sessionInfoTool,
|
||||||
searchTool,
|
|
||||||
schemaTool,
|
schemaTool,
|
||||||
// Full agent toolset
|
// Shared core + analysis + raw SQL
|
||||||
...AGENT_TOOL_REGISTRY,
|
...SHARED_TOOLS,
|
||||||
]
|
]
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in new issue
Block a user