UNPKG

autosnippet

Version:

Extract code patterns into a knowledge base for AI coding assistants

116 lines (115 loc) 4.68 kB
/** * AutoSnippet V3 MCP Server — 整合版 * * Model Context Protocol (stdio transport) * 提供给 IDE AI Agent (Cursor/VSCode Copilot) 的工具集 * * V3.3 整合:39 → 16 工具(14 agent + 2 admin) * 通过 ASD_MCP_TIER 环境变量控制可见工具集(agent/admin) * * 冷启动双路径: * - 外部 Agent 路径: bootstrap (Mission Briefing) → dimension_complete × N → wiki(plan) → wiki(finalize) * - 内部 Agent 路径: bootstrap.js bootstrapKnowledge() → orchestrator.js AI pipeline (Phase 5) * * Gateway 权限 gating: 写操作经过 Gateway 权限/宪法/审计检查(支持动态 resolver) * * 本文件仅包含服务编排层(初始化、路由、Gateway gating、生命周期)。 * 工具定义 → tools.js * Handler 实现 → handlers/*.js * 整合路由 → handlers/consolidated.js */ import { McpServer as SdkMcpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; import { CapabilityProbe } from '#core/capability/CapabilityProbe.js'; import Logger from '#infra/logging/Logger.js'; import type { IntentState, McpContext, McpServiceContainer } from './handlers/types.js'; /** MCP session tracking (with intent lifecycle) */ interface McpSession { id: string; startedAt: number; toolCallCount: number; toolsUsed: Set<string>; lastActivityAt: number; intent: IntentState; } /** McpServer constructor options */ interface McpServerOptions { container?: McpServiceContainer | null; bootstrap?: BootstrapLike | null; } /** Bootstrap instance minimal shape */ interface BootstrapLike { initialize(): Promise<Record<string, unknown>>; shutdown(): Promise<void>; } /** Tool handler function (sync or async, compatible with wrapHandler) */ type ToolHandlerFn = (ctx: McpContext, args: Record<string, unknown>) => Promise<unknown> | unknown; export declare class McpServer { container: McpServiceContainer | null; logger: ReturnType<typeof Logger.getInstance>; _autoApproveMarked: boolean; _capabilityProbe: CapabilityProbe | null; _lastTaskOperation: string; _session: McpSession; _startedAt: number; bootstrap: BootstrapLike | null; sdkServer: SdkMcpServer | null; constructor(options?: McpServerOptions); /** 共享上下文对象,传给所有 handler */ get _ctx(): { container: McpServiceContainer | null; logger: import("winston").Logger; startedAt: number; session: McpSession; }; initialize(): Promise<this>; /** * 注册 ListTools / CallTool 请求处理器 * ListTools 基于 ASD_MCP_TIER 过滤可见工具 */ _registerHandlers(): void; _handleToolCall(name: string, args: Record<string, unknown>): Promise<unknown>; /** * Post-tool-call hook: update session stats + intent behavior tracking. * Always called (non-blocking, synchronous). * * - Session stats: toolCallCount, toolsUsed, lastActivityAt * - Intent tracking (when active): toolCalls, searchQueries, mentionedFiles, drift detection */ _trackSession(toolName: string, result: unknown): void; /** * Inject active decisions + intent context into tool results. * Currently deferred — enable by uncommenting the call in _handleToolCall. */ _injectDecisions(toolName: string, result: unknown): Promise<unknown>; private _detectDrift; private _computeKeywordOverlap; private _computeOverlap; private _extractSearchQuery; private _extractMentionedFiles; private _inferModule; /** * 解析工具名到 handler 函数(V3 整合版) */ _resolveHandler(name: string): ToolHandlerFn | null; /** * 获取(或懒创建)CapabilityProbe 实例,用于探测子仓库写权限 * 配置来自 constitution capabilities.git_write */ _getCapabilityProbe(): CapabilityProbe; /** * Gateway 权限 gating — 写操作验证权限/宪法/审计 * 只读工具直接跳过(不在 TOOL_GATEWAY_MAP 中) * 支持动态 resolver(operation-based 工具按参数解析 action/resource) * * actor 解析:使用 CapabilityProbe 探测本地用户的子仓库权限 * - admin → 'developer' 全权限 * - contributor → 'contributor' 只读,写操作被拒绝 * - visitor → 'visitor' 最小权限 * 探测失败时降级为 'external_agent'(向后兼容) */ _gatewayGate(toolName: string, args: Record<string, unknown>): Promise<void>; start(): Promise<void>; shutdown(): Promise<void>; } export declare function startMcpServer(): Promise<McpServer>; export default McpServer;