mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-18 22:35:51 +00:00
feat: 工具层及 mcp 大重构 (#252)
* feat: 第一版大重构 * fix: 修复类型问题 * chore: 更新版本到 1.3.2 * Add brave as alternative WebSearchTool * fix: 修正顺序 * fix: 修复对穷鬼模式的 auto dream 和 session memory 越过 * feat: 穷鬼模式去除 session-summary * feat: 创建 builtin-tools 包,搬运所有工具实现 将 src/tools/ 下的全部 60 个工具目录迁移至 packages/builtin-tools/src/tools/, 内部导入路径已更新为 src/ alias 模式。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: 更新 src/ 中所有工具引用至 builtin-tools 包,删除 src/tools/ - src/tools.ts 及 178 个 src/ 文件的 import 路径从 ./tools/ 改为 builtin-tools/tools/ - 删除 src/tools/ 整个目录(已迁移至 packages/builtin-tools/) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * chore: 添加 builtin-tools 路径别名至 tsconfig,更新 bun.lock - tsconfig.json 新增 builtin-tools/* 和 builtin-tools 路径映射 - 新增 packages/builtin-tools/src 至 include Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: 为 builtin-tools、mcp-client、agent-tools 添加 @claude-code-best 作用域前缀 所有包名及 import 路径统一添加 @claude-code-best/ 前缀: - builtin-tools → @claude-code-best/builtin-tools - mcp-client → @claude-code-best/mcp-client - agent-tools → @claude-code-best/agent-tools Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: 修复 node 环境没有 bun 的问题 --------- Co-authored-by: Eric-Guo <eric.guocz@gmail.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -11,8 +11,9 @@
|
||||
*/
|
||||
|
||||
import type { TaskContext } from '../../Task.js'
|
||||
import { isPoorModeActive } from '../../commands/poor/poorMode.js'
|
||||
import { updateAgentSummary } from '../../tasks/LocalAgentTask/LocalAgentTask.js'
|
||||
import { filterIncompleteToolCalls } from '../../tools/AgentTool/runAgent.js'
|
||||
import { filterIncompleteToolCalls } from '@claude-code-best/builtin-tools/tools/AgentTool/runAgent.js'
|
||||
import type { AgentId } from '../../types/ids.js'
|
||||
import { logForDebugging } from '../../utils/debug.js'
|
||||
import {
|
||||
@@ -60,6 +61,11 @@ export function startAgentSummarization(
|
||||
|
||||
async function runSummary(): Promise<void> {
|
||||
if (stopped) return
|
||||
if (isPoorModeActive()) {
|
||||
logForDebugging('[AgentSummary] Skipping summary — poor mode active')
|
||||
scheduleNext()
|
||||
return
|
||||
}
|
||||
|
||||
logForDebugging(`[AgentSummary] Timer fired for agent ${agentId}`)
|
||||
|
||||
|
||||
@@ -7,14 +7,14 @@
|
||||
*/
|
||||
|
||||
import type { Tool, ToolUseContext } from '../../Tool.js'
|
||||
import type { BuiltInAgentDefinition } from '../../tools/AgentTool/loadAgentsDir.js'
|
||||
import { runAgent } from '../../tools/AgentTool/runAgent.js'
|
||||
import { FILE_EDIT_TOOL_NAME } from '../../tools/FileEditTool/constants.js'
|
||||
import type { BuiltInAgentDefinition } from '@claude-code-best/builtin-tools/tools/AgentTool/loadAgentsDir.js'
|
||||
import { runAgent } from '@claude-code-best/builtin-tools/tools/AgentTool/runAgent.js'
|
||||
import { FILE_EDIT_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/FileEditTool/constants.js'
|
||||
import {
|
||||
FileReadTool,
|
||||
type Output as FileReadToolOutput,
|
||||
registerFileReadListener,
|
||||
} from '../../tools/FileReadTool/FileReadTool.js'
|
||||
} from '@claude-code-best/builtin-tools/tools/FileReadTool/FileReadTool.js'
|
||||
import { isFsInaccessible } from '../../utils/errors.js'
|
||||
import { cloneFileStateCache } from '../../utils/fileStateCache.js'
|
||||
import {
|
||||
|
||||
@@ -10,8 +10,8 @@ import {
|
||||
type SpeculationResult,
|
||||
type SpeculationState,
|
||||
} from '../../state/AppStateStore.js'
|
||||
import { commandHasAnyCd } from '../../tools/BashTool/bashPermissions.js'
|
||||
import { checkReadOnlyConstraints } from '../../tools/BashTool/readOnlyValidation.js'
|
||||
import { commandHasAnyCd } from '@claude-code-best/builtin-tools/tools/BashTool/bashPermissions.js'
|
||||
import { checkReadOnlyConstraints } from '@claude-code-best/builtin-tools/tools/BashTool/readOnlyValidation.js'
|
||||
import type { SpeculationAcceptMessage } from '../../types/logs.js'
|
||||
import type { Message } from '../../types/message.js'
|
||||
import { createChildAbortController } from '../../utils/abortController.js'
|
||||
|
||||
@@ -12,11 +12,11 @@ import { getSystemPrompt } from '../../constants/prompts.js'
|
||||
import { getSystemContext, getUserContext } from '../../context.js'
|
||||
import type { CanUseToolFn } from '../../hooks/useCanUseTool.js'
|
||||
import type { Tool, ToolUseContext } from '../../Tool.js'
|
||||
import { FILE_EDIT_TOOL_NAME } from '../../tools/FileEditTool/constants.js'
|
||||
import { FILE_EDIT_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/FileEditTool/constants.js'
|
||||
import {
|
||||
FileReadTool,
|
||||
type Output as FileReadToolOutput,
|
||||
} from '../../tools/FileReadTool/FileReadTool.js'
|
||||
} from '@claude-code-best/builtin-tools/tools/FileReadTool/FileReadTool.js'
|
||||
import type { Message } from '../../types/message.js'
|
||||
import { count } from '../../utils/array.js'
|
||||
import {
|
||||
|
||||
@@ -36,7 +36,7 @@ import {
|
||||
type Tools,
|
||||
toolMatchesName,
|
||||
} from '../../Tool.js'
|
||||
import type { AgentDefinition } from '../../tools/AgentTool/loadAgentsDir.js'
|
||||
import type { AgentDefinition } from '@claude-code-best/builtin-tools/tools/AgentTool/loadAgentsDir.js'
|
||||
import {
|
||||
type ConnectorTextBlock,
|
||||
type ConnectorTextDelta,
|
||||
@@ -195,7 +195,7 @@ import {
|
||||
formatDeferredToolLine,
|
||||
isDeferredTool,
|
||||
TOOL_SEARCH_TOOL_NAME,
|
||||
} from '../../tools/ToolSearchTool/prompt.js'
|
||||
} from '@claude-code-best/builtin-tools/tools/ToolSearchTool/prompt.js'
|
||||
import { count } from '../../utils/array.js'
|
||||
import { insertBlockAfterToolResults } from '../../utils/contentArray.js'
|
||||
import { validateBoundedIntEnvVar } from '../../utils/envValidation.js'
|
||||
|
||||
@@ -46,7 +46,7 @@ import {
|
||||
import {
|
||||
isDeferredTool,
|
||||
TOOL_SEARCH_TOOL_NAME,
|
||||
} from '../../../tools/ToolSearchTool/prompt.js'
|
||||
} from '@claude-code-best/builtin-tools/tools/ToolSearchTool/prompt.js'
|
||||
|
||||
/**
|
||||
* Detect whether DeepSeek-style thinking mode should be enabled.
|
||||
|
||||
@@ -49,8 +49,8 @@ import {
|
||||
failDreamTask,
|
||||
isDreamTask,
|
||||
} from '../../tasks/DreamTask/DreamTask.js'
|
||||
import { FILE_EDIT_TOOL_NAME } from '../../tools/FileEditTool/constants.js'
|
||||
import { FILE_WRITE_TOOL_NAME } from '../../tools/FileWriteTool/prompt.js'
|
||||
import { FILE_EDIT_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/FileEditTool/constants.js'
|
||||
import { FILE_WRITE_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/FileWriteTool/prompt.js'
|
||||
|
||||
// Scan throttle: when time-gate passes but session-gate doesn't, the lock
|
||||
// mtime doesn't advance, so the time-gate keeps passing every turn.
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { FILE_EDIT_TOOL_NAME } from 'src/tools/FileEditTool/constants.js'
|
||||
import { FILE_READ_TOOL_NAME } from 'src/tools/FileReadTool/prompt.js'
|
||||
import { FILE_WRITE_TOOL_NAME } from 'src/tools/FileWriteTool/prompt.js'
|
||||
import { GLOB_TOOL_NAME } from 'src/tools/GlobTool/prompt.js'
|
||||
import { GREP_TOOL_NAME } from 'src/tools/GrepTool/prompt.js'
|
||||
import { NOTEBOOK_EDIT_TOOL_NAME } from 'src/tools/NotebookEditTool/constants.js'
|
||||
import { WEB_FETCH_TOOL_NAME } from 'src/tools/WebFetchTool/prompt.js'
|
||||
import { WEB_SEARCH_TOOL_NAME } from 'src/tools/WebSearchTool/prompt.js'
|
||||
import { FILE_EDIT_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/FileEditTool/constants.js'
|
||||
import { FILE_READ_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/FileReadTool/prompt.js'
|
||||
import { FILE_WRITE_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/FileWriteTool/prompt.js'
|
||||
import { GLOB_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/GlobTool/prompt.js'
|
||||
import { GREP_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/GrepTool/prompt.js'
|
||||
import { NOTEBOOK_EDIT_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/NotebookEditTool/constants.js'
|
||||
import { WEB_FETCH_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/WebFetchTool/prompt.js'
|
||||
import { WEB_SEARCH_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/WebSearchTool/prompt.js'
|
||||
import { SHELL_TOOL_NAMES } from 'src/utils/shell/shellToolUtils.js'
|
||||
import { isEnvTruthy } from '../../utils/envUtils.js'
|
||||
|
||||
|
||||
@@ -14,12 +14,12 @@ import type { QuerySource } from '../../constants/querySource.js'
|
||||
import type { CanUseToolFn } from '../../hooks/useCanUseTool.js'
|
||||
import type { Tool, ToolUseContext } from '../../Tool.js'
|
||||
import type { LocalAgentTaskState } from '../../tasks/LocalAgentTask/LocalAgentTask.js'
|
||||
import { FileReadTool } from '../../tools/FileReadTool/FileReadTool.js'
|
||||
import { FileReadTool } from '@claude-code-best/builtin-tools/tools/FileReadTool/FileReadTool.js'
|
||||
import {
|
||||
FILE_READ_TOOL_NAME,
|
||||
FILE_UNCHANGED_STUB,
|
||||
} from '../../tools/FileReadTool/prompt.js'
|
||||
import { ToolSearchTool } from '../../tools/ToolSearchTool/ToolSearchTool.js'
|
||||
} from '@claude-code-best/builtin-tools/tools/FileReadTool/prompt.js'
|
||||
import { ToolSearchTool } from '@claude-code-best/builtin-tools/tools/ToolSearchTool/ToolSearchTool.js'
|
||||
import type { AgentId } from '../../types/ids.js'
|
||||
import type {
|
||||
AssistantMessage,
|
||||
|
||||
@@ -2,13 +2,13 @@ import { feature } from 'bun:bundle'
|
||||
import type { ToolResultBlockParam } from '@anthropic-ai/sdk/resources/index.mjs'
|
||||
import type { QuerySource } from '../../constants/querySource.js'
|
||||
import type { ToolUseContext } from '../../Tool.js'
|
||||
import { FILE_EDIT_TOOL_NAME } from '../../tools/FileEditTool/constants.js'
|
||||
import { FILE_READ_TOOL_NAME } from '../../tools/FileReadTool/prompt.js'
|
||||
import { FILE_WRITE_TOOL_NAME } from '../../tools/FileWriteTool/prompt.js'
|
||||
import { GLOB_TOOL_NAME } from '../../tools/GlobTool/prompt.js'
|
||||
import { GREP_TOOL_NAME } from '../../tools/GrepTool/prompt.js'
|
||||
import { WEB_FETCH_TOOL_NAME } from '../../tools/WebFetchTool/prompt.js'
|
||||
import { WEB_SEARCH_TOOL_NAME } from '../../tools/WebSearchTool/prompt.js'
|
||||
import { FILE_EDIT_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/FileEditTool/constants.js'
|
||||
import { FILE_READ_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/FileReadTool/prompt.js'
|
||||
import { FILE_WRITE_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/FileWriteTool/prompt.js'
|
||||
import { GLOB_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/GlobTool/prompt.js'
|
||||
import { GREP_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/GrepTool/prompt.js'
|
||||
import { WEB_FETCH_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/WebFetchTool/prompt.js'
|
||||
import { WEB_SEARCH_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/WebSearchTool/prompt.js'
|
||||
import type { Message } from '../../types/message.js'
|
||||
import { logForDebugging } from '../../utils/debug.js'
|
||||
import { getMainLoopModel } from '../../utils/model/model.js'
|
||||
|
||||
@@ -2,7 +2,7 @@ import { feature } from 'bun:bundle'
|
||||
import type { QuerySource } from '../../constants/querySource.js'
|
||||
import { clearSystemPromptSections } from '../../constants/systemPromptSections.js'
|
||||
import { getUserContext } from '../../context.js'
|
||||
import { clearSpeculativeChecks } from '../../tools/BashTool/bashPermissions.js'
|
||||
import { clearSpeculativeChecks } from '@claude-code-best/builtin-tools/tools/BashTool/bashPermissions.js'
|
||||
import { clearClassifierApprovals } from '../../utils/classifierApprovals.js'
|
||||
import { resetGetMemoryFilesCache } from '../../utils/claudemd.js'
|
||||
import { clearSessionMessagesCache } from '../../utils/sessionStorage.js'
|
||||
|
||||
@@ -28,13 +28,13 @@ import {
|
||||
isAutoMemPath,
|
||||
} from '../../memdir/paths.js'
|
||||
import type { Tool } from '../../Tool.js'
|
||||
import { BASH_TOOL_NAME } from '../../tools/BashTool/toolName.js'
|
||||
import { FILE_EDIT_TOOL_NAME } from '../../tools/FileEditTool/constants.js'
|
||||
import { FILE_READ_TOOL_NAME } from '../../tools/FileReadTool/prompt.js'
|
||||
import { FILE_WRITE_TOOL_NAME } from '../../tools/FileWriteTool/prompt.js'
|
||||
import { GLOB_TOOL_NAME } from '../../tools/GlobTool/prompt.js'
|
||||
import { GREP_TOOL_NAME } from '../../tools/GrepTool/prompt.js'
|
||||
import { REPL_TOOL_NAME } from '../../tools/REPLTool/constants.js'
|
||||
import { BASH_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/BashTool/toolName.js'
|
||||
import { FILE_EDIT_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/FileEditTool/constants.js'
|
||||
import { FILE_READ_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/FileReadTool/prompt.js'
|
||||
import { FILE_WRITE_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/FileWriteTool/prompt.js'
|
||||
import { GLOB_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/GlobTool/prompt.js'
|
||||
import { GREP_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/GrepTool/prompt.js'
|
||||
import { REPL_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/REPLTool/constants.js'
|
||||
import type {
|
||||
AssistantMessage,
|
||||
Message,
|
||||
|
||||
@@ -16,12 +16,12 @@ import {
|
||||
TYPES_SECTION_INDIVIDUAL,
|
||||
WHAT_NOT_TO_SAVE_SECTION,
|
||||
} from '../../memdir/memoryTypes.js'
|
||||
import { BASH_TOOL_NAME } from '../../tools/BashTool/toolName.js'
|
||||
import { FILE_EDIT_TOOL_NAME } from '../../tools/FileEditTool/constants.js'
|
||||
import { FILE_READ_TOOL_NAME } from '../../tools/FileReadTool/prompt.js'
|
||||
import { FILE_WRITE_TOOL_NAME } from '../../tools/FileWriteTool/prompt.js'
|
||||
import { GLOB_TOOL_NAME } from '../../tools/GlobTool/prompt.js'
|
||||
import { GREP_TOOL_NAME } from '../../tools/GrepTool/prompt.js'
|
||||
import { BASH_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/BashTool/toolName.js'
|
||||
import { FILE_EDIT_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/FileEditTool/constants.js'
|
||||
import { FILE_READ_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/FileReadTool/prompt.js'
|
||||
import { FILE_WRITE_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/FileWriteTool/prompt.js'
|
||||
import { GLOB_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/GlobTool/prompt.js'
|
||||
import { GREP_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/GrepTool/prompt.js'
|
||||
|
||||
/**
|
||||
* Shared opener for both extract-prompt variants.
|
||||
|
||||
18
src/services/mcp/adapter/analytics.ts
Normal file
18
src/services/mcp/adapter/analytics.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
// Host analytics adapter — bridges logEvent to mcp-client's AnalyticsSink interface
|
||||
|
||||
import type { AnalyticsSink } from '@claude-code-best/mcp-client'
|
||||
import {
|
||||
type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
||||
logEvent,
|
||||
} from '../../analytics/index.js'
|
||||
|
||||
/**
|
||||
* Creates an AnalyticsSink implementation that delegates to the host's logEvent.
|
||||
*/
|
||||
export function createMcpAnalytics(): AnalyticsSink {
|
||||
return {
|
||||
trackEvent(event: string, metadata: Record<string, unknown>) {
|
||||
logEvent(event, metadata as Record<string, AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS>)
|
||||
},
|
||||
}
|
||||
}
|
||||
28
src/services/mcp/adapter/auth.ts
Normal file
28
src/services/mcp/adapter/auth.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
// Host auth provider adapter — bridges OAuth token management to mcp-client's AuthProvider interface
|
||||
|
||||
import type { AuthProvider } from '@claude-code-best/mcp-client'
|
||||
import {
|
||||
getClaudeAIOAuthTokens,
|
||||
checkAndRefreshOAuthTokenIfNeeded,
|
||||
handleOAuth401Error,
|
||||
} from '../../../utils/auth.js'
|
||||
|
||||
/**
|
||||
* Creates an AuthProvider implementation using the host's OAuth token management.
|
||||
*/
|
||||
export function createMcpAuth(): AuthProvider {
|
||||
return {
|
||||
async getTokens() {
|
||||
const tokens = getClaudeAIOAuthTokens()
|
||||
if (!tokens) return null
|
||||
return { accessToken: tokens.accessToken }
|
||||
},
|
||||
async refreshTokens() {
|
||||
await checkAndRefreshOAuthTokenIfNeeded()
|
||||
},
|
||||
async handleOAuthError(error: unknown) {
|
||||
const currentToken = getClaudeAIOAuthTokens()?.accessToken ?? ''
|
||||
await handleOAuth401Error(currentToken)
|
||||
},
|
||||
}
|
||||
}
|
||||
15
src/services/mcp/adapter/featureGate.ts
Normal file
15
src/services/mcp/adapter/featureGate.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
// Host feature gate adapter — bridges feature() to mcp-client's FeatureGate interface
|
||||
|
||||
import type { FeatureGate } from '@claude-code-best/mcp-client'
|
||||
import { feature } from 'bun:bundle'
|
||||
|
||||
/**
|
||||
* Creates a FeatureGate implementation using the host's feature flag system.
|
||||
*/
|
||||
export function createMcpFeatureGate(): FeatureGate {
|
||||
return {
|
||||
isEnabled(flag: string) {
|
||||
return feature(flag)
|
||||
},
|
||||
}
|
||||
}
|
||||
15
src/services/mcp/adapter/httpConfig.ts
Normal file
15
src/services/mcp/adapter/httpConfig.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
// Host HTTP config adapter — bridges getUserAgent/getSessionId to mcp-client's HttpConfig interface
|
||||
|
||||
import type { HttpConfig } from '@claude-code-best/mcp-client'
|
||||
import { getMCPUserAgent } from '../../../utils/http.js'
|
||||
import { getSessionId } from '../../../bootstrap/state.js'
|
||||
|
||||
/**
|
||||
* Creates an HttpConfig implementation using the host's user agent and session ID.
|
||||
*/
|
||||
export function createMcpHttpConfig(): HttpConfig {
|
||||
return {
|
||||
getUserAgent: () => getMCPUserAgent(),
|
||||
getSessionId: () => getSessionId(),
|
||||
}
|
||||
}
|
||||
16
src/services/mcp/adapter/imageProcessor.ts
Normal file
16
src/services/mcp/adapter/imageProcessor.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
// Host image processor adapter — bridges maybeResizeAndDownsampleImageBuffer to mcp-client's ImageProcessor interface
|
||||
|
||||
import type { ImageProcessor } from '@claude-code-best/mcp-client'
|
||||
import { maybeResizeAndDownsampleImageBuffer } from '../../../utils/imageResizer.js'
|
||||
|
||||
/**
|
||||
* Creates an ImageProcessor implementation using the host's image resizing.
|
||||
*/
|
||||
export function createMcpImageProcessor(): ImageProcessor {
|
||||
return {
|
||||
async resizeAndDownsample(buffer: Buffer) {
|
||||
const result = await maybeResizeAndDownsampleImageBuffer(buffer, buffer.length, 'png')
|
||||
return result.buffer
|
||||
},
|
||||
}
|
||||
}
|
||||
32
src/services/mcp/adapter/index.ts
Normal file
32
src/services/mcp/adapter/index.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
// Host dependency injection — assembles McpClientDependencies from host infrastructure
|
||||
// This is the single entry point for creating the dependencies object used by createMcpManager()
|
||||
|
||||
import type { McpClientDependencies } from '@claude-code-best/mcp-client'
|
||||
import { createMcpLogger } from './logger.js'
|
||||
import { createMcpHttpConfig } from './httpConfig.js'
|
||||
import { createMcpProxyConfig } from './proxy.js'
|
||||
import { createMcpAnalytics } from './analytics.js'
|
||||
import { createMcpSubprocessEnv } from './subprocessEnv.js'
|
||||
import { createMcpStorage } from './storage.js'
|
||||
import { createMcpImageProcessor } from './imageProcessor.js'
|
||||
import { createMcpAuth } from './auth.js'
|
||||
/**
|
||||
* Creates the full set of MCP client dependencies using host infrastructure.
|
||||
* All adapters are lazy — they only call into host modules when invoked.
|
||||
*
|
||||
* Note: featureGate is omitted because Bun's feature() requires string-literal
|
||||
* arguments at compile time and cannot accept runtime variables. The interface
|
||||
* field is optional and the mcp-client package does not use it currently.
|
||||
*/
|
||||
export function createMcpDependencies(): McpClientDependencies {
|
||||
return {
|
||||
logger: createMcpLogger(),
|
||||
httpConfig: createMcpHttpConfig(),
|
||||
proxy: createMcpProxyConfig(),
|
||||
analytics: createMcpAnalytics(),
|
||||
subprocessEnv: createMcpSubprocessEnv(),
|
||||
storage: createMcpStorage(),
|
||||
imageProcessor: createMcpImageProcessor(),
|
||||
auth: createMcpAuth(),
|
||||
}
|
||||
}
|
||||
38
src/services/mcp/adapter/logger.ts
Normal file
38
src/services/mcp/adapter/logger.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
// Host logger adapter — bridges logMCPDebug/logMCPError to mcp-client's Logger interface
|
||||
|
||||
import type { Logger } from '@claude-code-best/mcp-client'
|
||||
import { logMCPDebug, logMCPError } from '../../../utils/log.js'
|
||||
|
||||
/**
|
||||
* Creates a Logger implementation that delegates to the host's MCP logging system.
|
||||
*/
|
||||
export function createMcpLogger(): Logger {
|
||||
return {
|
||||
debug(message: string, ...args: unknown[]) {
|
||||
// Extract server name from bracketed prefix if present: [serverName] message
|
||||
const match = message.match(/^\[([^\]]+)\]\s*(.*)/)
|
||||
if (match) {
|
||||
logMCPDebug(match[1], match[2])
|
||||
}
|
||||
// Silently ignore messages without server name prefix
|
||||
},
|
||||
info(message: string, ...args: unknown[]) {
|
||||
const match = message.match(/^\[([^\]]+)\]\s*(.*)/)
|
||||
if (match) {
|
||||
logMCPDebug(match[1], match[2])
|
||||
}
|
||||
},
|
||||
warn(message: string, ...args: unknown[]) {
|
||||
const match = message.match(/^\[([^\]]+)\]\s*(.*)/)
|
||||
if (match) {
|
||||
logMCPError(match[1], message)
|
||||
}
|
||||
},
|
||||
error(message: string, ...args: unknown[]) {
|
||||
const match = message.match(/^\[([^\]]+)\]\s*(.*)/)
|
||||
if (match) {
|
||||
logMCPError(match[1], args[0] ?? message)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
30
src/services/mcp/adapter/proxy.ts
Normal file
30
src/services/mcp/adapter/proxy.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
// Host proxy config adapter — bridges proxy/MTLS to mcp-client's ProxyConfig interface
|
||||
|
||||
import type { ProxyConfig } from '@claude-code-best/mcp-client'
|
||||
import {
|
||||
getProxyFetchOptions,
|
||||
getWebSocketProxyAgent,
|
||||
getWebSocketProxyUrl,
|
||||
} from '../../../utils/proxy.js'
|
||||
import { getWebSocketTLSOptions } from '../../../utils/mtls.js'
|
||||
|
||||
/**
|
||||
* Creates a ProxyConfig implementation using the host's proxy and TLS settings.
|
||||
*/
|
||||
export function createMcpProxyConfig(): ProxyConfig {
|
||||
return {
|
||||
getFetchOptions() {
|
||||
return getProxyFetchOptions() as Record<string, unknown>
|
||||
},
|
||||
getWebSocketAgent(url: string) {
|
||||
return getWebSocketProxyAgent(url)
|
||||
},
|
||||
getWebSocketUrl(url: string) {
|
||||
return getWebSocketProxyUrl(url)
|
||||
},
|
||||
getTLSOptions() {
|
||||
const opts = getWebSocketTLSOptions()
|
||||
return opts as Record<string, unknown> | undefined
|
||||
},
|
||||
}
|
||||
}
|
||||
20
src/services/mcp/adapter/storage.ts
Normal file
20
src/services/mcp/adapter/storage.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
// Host content storage adapter — bridges persistBinaryContent to mcp-client's ContentStorage interface
|
||||
|
||||
import type { ContentStorage } from '@claude-code-best/mcp-client'
|
||||
import { persistBinaryContent } from '../../../utils/mcpOutputStorage.js'
|
||||
import { persistToolResult, isPersistError } from '../../../utils/toolResultStorage.js'
|
||||
|
||||
/**
|
||||
* Creates a ContentStorage implementation using the host's binary persistence.
|
||||
*/
|
||||
export function createMcpStorage(): ContentStorage {
|
||||
return {
|
||||
async persistBinaryContent(data: Buffer, ext: string) {
|
||||
const result = await persistBinaryContent(data, ext, `mcp-adapter-${Date.now()}`)
|
||||
if ('error' in result) {
|
||||
throw new Error(result.error)
|
||||
}
|
||||
return result.filepath
|
||||
},
|
||||
}
|
||||
}
|
||||
15
src/services/mcp/adapter/subprocessEnv.ts
Normal file
15
src/services/mcp/adapter/subprocessEnv.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
// Host subprocess environment adapter
|
||||
|
||||
import type { SubprocessEnvProvider } from '@claude-code-best/mcp-client'
|
||||
import { subprocessEnv } from '../../../utils/subprocessEnv.js'
|
||||
|
||||
/**
|
||||
* Creates a SubprocessEnvProvider using the host's subprocess environment logic.
|
||||
*/
|
||||
export function createMcpSubprocessEnv(): SubprocessEnvProvider {
|
||||
return {
|
||||
getEnv(additional?: Record<string, string>) {
|
||||
return { ...subprocessEnv(), ...additional } as Record<string, string>
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -50,10 +50,10 @@ import {
|
||||
type ToolCallProgress,
|
||||
toolMatchesName,
|
||||
} from '../../Tool.js'
|
||||
import { ListMcpResourcesTool } from '../../tools/ListMcpResourcesTool/ListMcpResourcesTool.js'
|
||||
import { type MCPProgress, MCPTool } from '../../tools/MCPTool/MCPTool.js'
|
||||
import { createMcpAuthTool } from '../../tools/McpAuthTool/McpAuthTool.js'
|
||||
import { ReadMcpResourceTool } from '../../tools/ReadMcpResourceTool/ReadMcpResourceTool.js'
|
||||
import { ListMcpResourcesTool } from '@claude-code-best/builtin-tools/tools/ListMcpResourcesTool/ListMcpResourcesTool.js'
|
||||
import { type MCPProgress, MCPTool } from '@claude-code-best/builtin-tools/tools/MCPTool/MCPTool.js'
|
||||
import { createMcpAuthTool } from '@claude-code-best/builtin-tools/tools/McpAuthTool/McpAuthTool.js'
|
||||
import { ReadMcpResourceTool } from '@claude-code-best/builtin-tools/tools/ReadMcpResourceTool/ReadMcpResourceTool.js'
|
||||
import { createAbortController } from '../../utils/abortController.js'
|
||||
import { count } from '../../utils/array.js'
|
||||
import {
|
||||
@@ -93,7 +93,6 @@ import {
|
||||
getWebSocketProxyAgent,
|
||||
getWebSocketProxyUrl,
|
||||
} from '../../utils/proxy.js'
|
||||
import { recursivelySanitizeUnicode } from '../../utils/sanitization.js'
|
||||
import { getSessionIngressAuthToken } from '../../utils/sessionIngressAuth.js'
|
||||
import { subprocessEnv } from '../../utils/subprocessEnv.js'
|
||||
import {
|
||||
@@ -113,6 +112,19 @@ import { buildMcpToolName } from './mcpStringUtils.js'
|
||||
import { normalizeNameForMCP } from './normalization.js'
|
||||
import { getLoggingSafeMcpBaseUrl } from './utils.js'
|
||||
|
||||
// Package imports — delegate to mcp-client package utilities where applicable
|
||||
import {
|
||||
createMcpClient as createMcpClientFromPackage,
|
||||
captureStderr,
|
||||
isMcpSessionExpiredError as isMcpSessionExpiredErrorFromPackage,
|
||||
installConnectionMonitor,
|
||||
createCleanup as createCleanupFromPackage,
|
||||
buildConnectedServer,
|
||||
DEFAULT_CONNECTION_TIMEOUT_MS,
|
||||
MAX_MCP_DESCRIPTION_LENGTH as PKG_MAX_MCP_DESCRIPTION_LENGTH,
|
||||
} from '@claude-code-best/mcp-client'
|
||||
import { recursivelySanitizeUnicode } from '@claude-code-best/mcp-client'
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-require-imports */
|
||||
const fetchMcpSkillsForClient = feature('MCP_SKILLS')
|
||||
? (
|
||||
@@ -123,7 +135,7 @@ const fetchMcpSkillsForClient = feature('MCP_SKILLS')
|
||||
import { UnauthorizedError } from '@modelcontextprotocol/sdk/client/auth.js'
|
||||
import type { AssistantMessage } from 'src/types/message.js'
|
||||
/* eslint-enable @typescript-eslint/no-require-imports */
|
||||
import { classifyMcpToolForCollapse } from '../../tools/MCPTool/classifyForCollapse.js'
|
||||
import { classifyMcpToolForCollapse } from '@claude-code-best/builtin-tools/tools/MCPTool/classifyForCollapse.js'
|
||||
import { clearKeychainCache } from '../../utils/secureStorage/macOsKeychainHelpers.js'
|
||||
import { sleep } from '../../utils/sleep.js'
|
||||
import {
|
||||
@@ -191,20 +203,7 @@ export class McpToolCallError_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS extends T
|
||||
* Per the MCP spec, servers return 404 when a session ID is no longer valid.
|
||||
* We check both signals to avoid false positives from generic 404s (wrong URL, server gone, etc.).
|
||||
*/
|
||||
export function isMcpSessionExpiredError(error: Error): boolean {
|
||||
const httpStatus =
|
||||
'code' in error ? (error as Error & { code?: number }).code : undefined
|
||||
if (httpStatus !== 404) {
|
||||
return false
|
||||
}
|
||||
// The SDK embeds the response body text in the error message.
|
||||
// MCP servers return: {"error":{"code":-32001,"message":"Session not found"},...}
|
||||
// Check for the JSON-RPC error code to distinguish from generic web server 404s.
|
||||
return (
|
||||
error.message.includes('"code":-32001') ||
|
||||
error.message.includes('"code": -32001')
|
||||
)
|
||||
}
|
||||
export const isMcpSessionExpiredError = isMcpSessionExpiredErrorFromPackage
|
||||
|
||||
/**
|
||||
* Default timeout for MCP tool calls (effectively infinite - ~27.8 hours).
|
||||
@@ -216,7 +215,7 @@ const DEFAULT_MCP_TOOL_TIMEOUT_MS = 100_000_000
|
||||
* OpenAPI-generated MCP servers have been observed dumping 15-60KB of endpoint
|
||||
* docs into tool.description; this caps the p95 tail without losing the intent.
|
||||
*/
|
||||
const MAX_MCP_DESCRIPTION_LENGTH = 2048
|
||||
const MAX_MCP_DESCRIPTION_LENGTH = PKG_MAX_MCP_DESCRIPTION_LENGTH
|
||||
|
||||
/**
|
||||
* Gets the timeout for MCP tool calls in milliseconds.
|
||||
|
||||
@@ -21,7 +21,7 @@ export const ConfigScopeSchema = lazySchema(() =>
|
||||
export type ConfigScope = z.infer<ReturnType<typeof ConfigScopeSchema>>
|
||||
|
||||
export const TransportSchema = lazySchema(() =>
|
||||
z.enum(['stdio', 'sse', 'sse-ide', 'http', 'ws', 'sdk']),
|
||||
z.enum(['stdio', 'sse', 'sse-ide', 'http', 'ws', 'sdk', 'claudeai-proxy']),
|
||||
)
|
||||
export type Transport = z.infer<ReturnType<typeof TransportSchema>>
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import { getIsNonInteractiveSession } from '../../bootstrap/state.js'
|
||||
import type { Command } from '../../commands.js'
|
||||
import type { AgentMcpServerInfo } from '../../components/mcp/types.js'
|
||||
import type { Tool } from '../../Tool.js'
|
||||
import type { AgentDefinition } from '../../tools/AgentTool/loadAgentsDir.js'
|
||||
import type { AgentDefinition } from '@claude-code-best/builtin-tools/tools/AgentTool/loadAgentsDir.js'
|
||||
import { getCwd } from '../../utils/cwd.js'
|
||||
import { getGlobalClaudeFile } from '../../utils/env.js'
|
||||
import { isSettingSourceEnabled } from '../../utils/settings/constants.js'
|
||||
|
||||
@@ -11,7 +11,7 @@ import { getDesktopUpsellConfig } from '../../components/DesktopUpsell/DesktopUp
|
||||
import { color } from '@anthropic/ink'
|
||||
import { shouldShowOverageCreditUpsell } from '../../components/LogoV2/OverageCreditUpsell.js'
|
||||
import { getShortcutDisplay } from '../../keybindings/shortcutFormat.js'
|
||||
import { isKairosCronEnabled } from '../../tools/ScheduleCronTool/prompt.js'
|
||||
import { isKairosCronEnabled } from '@claude-code-best/builtin-tools/tools/ScheduleCronTool/prompt.js'
|
||||
import { is1PApiCustomer } from '../../utils/auth.js'
|
||||
import { countConcurrentSessions } from '../../utils/concurrentSessions.js'
|
||||
import { getGlobalConfig } from '../../utils/config.js'
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
} from 'src/utils/messages.js'
|
||||
import type { CanUseToolFn } from '../../hooks/useCanUseTool.js'
|
||||
import { findToolByName, type Tools, type ToolUseContext } from '../../Tool.js'
|
||||
import { BASH_TOOL_NAME } from '../../tools/BashTool/toolName.js'
|
||||
import { BASH_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/BashTool/toolName.js'
|
||||
import type { AssistantMessage, Message } from '../../types/message.js'
|
||||
import { createChildAbortController } from '../../utils/abortController.js'
|
||||
import { runToolUse } from './toolExecution.js'
|
||||
|
||||
@@ -35,19 +35,19 @@ import {
|
||||
type ToolProgressData,
|
||||
type ToolUseContext,
|
||||
} from '../../Tool.js'
|
||||
import type { BashToolInput } from '../../tools/BashTool/BashTool.js'
|
||||
import { startSpeculativeClassifierCheck } from '../../tools/BashTool/bashPermissions.js'
|
||||
import { BASH_TOOL_NAME } from '../../tools/BashTool/toolName.js'
|
||||
import { FILE_EDIT_TOOL_NAME } from '../../tools/FileEditTool/constants.js'
|
||||
import { FILE_READ_TOOL_NAME } from '../../tools/FileReadTool/prompt.js'
|
||||
import { FILE_WRITE_TOOL_NAME } from '../../tools/FileWriteTool/prompt.js'
|
||||
import { NOTEBOOK_EDIT_TOOL_NAME } from '../../tools/NotebookEditTool/constants.js'
|
||||
import { POWERSHELL_TOOL_NAME } from '../../tools/PowerShellTool/toolName.js'
|
||||
import { parseGitCommitId } from '../../tools/shared/gitOperationTracking.js'
|
||||
import type { BashToolInput } from '@claude-code-best/builtin-tools/tools/BashTool/BashTool.js'
|
||||
import { startSpeculativeClassifierCheck } from '@claude-code-best/builtin-tools/tools/BashTool/bashPermissions.js'
|
||||
import { BASH_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/BashTool/toolName.js'
|
||||
import { FILE_EDIT_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/FileEditTool/constants.js'
|
||||
import { FILE_READ_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/FileReadTool/prompt.js'
|
||||
import { FILE_WRITE_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/FileWriteTool/prompt.js'
|
||||
import { NOTEBOOK_EDIT_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/NotebookEditTool/constants.js'
|
||||
import { POWERSHELL_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/PowerShellTool/toolName.js'
|
||||
import { parseGitCommitId } from '@claude-code-best/builtin-tools/tools/shared/gitOperationTracking.js'
|
||||
import {
|
||||
isDeferredTool,
|
||||
TOOL_SEARCH_TOOL_NAME,
|
||||
} from '../../tools/ToolSearchTool/prompt.js'
|
||||
} from '@claude-code-best/builtin-tools/tools/ToolSearchTool/prompt.js'
|
||||
import { getAllBaseTools } from '../../tools.js'
|
||||
import type { HookProgress } from '../../types/hooks.js'
|
||||
import { recordToolObservation } from '../langfuse/index.js'
|
||||
|
||||
Reference in New Issue
Block a user