cherry-studio/packages/shared/mcp.ts
Vaayne 7a5b011bfa feat: add available tools section to HUB_MODE_SYSTEM_PROMPT
- Add shared utility for generating MCP tool function names (serverName_toolName format)
- Update hub server to use consistent function naming across search, exec and prompt
- Add fetchAllActiveServerTools to ApiService for renderer process
- Update parameterBuilder to include available tools in auto/hub mode prompt
- Use CacheService for 1-minute tools caching in hub server
- Remove ToolRegistry in favor of direct fetching with caching
- Update search ranking to include server name matching
- Fix tests to use new naming format

Amp-Thread-ID: https://ampcode.com/threads/T-019b6971-d5c9-7719-9245-a89390078647
Co-authored-by: Amp <amp@ampcode.com>
2025-12-29 18:51:15 +08:00

44 lines
1.1 KiB
TypeScript

/**
* Convert a string to camelCase, ensuring it's a valid JavaScript identifier.
*/
export function toCamelCase(str: string): string {
let result = str
.replace(/[^a-zA-Z0-9]+(.)/g, (_, char) => char.toUpperCase())
.replace(/^[A-Z]/, (char) => char.toLowerCase())
.replace(/[^a-zA-Z0-9]/g, '')
// Ensure valid JS identifier: must start with letter or underscore
if (result && !/^[a-zA-Z_]/.test(result)) {
result = '_' + result
}
return result
}
/**
* Generate a unique function name from server name and tool name.
* Format: serverName_toolName (camelCase)
*/
export function generateMcpToolFunctionName(
serverName: string | undefined,
toolName: string,
existingNames?: Set<string>
): string {
const serverPrefix = serverName ? toCamelCase(serverName) : ''
const toolNameCamel = toCamelCase(toolName)
const baseName = serverPrefix ? `${serverPrefix}_${toolNameCamel}` : toolNameCamel
if (!existingNames) {
return baseName
}
let name = baseName
let counter = 1
while (existingNames.has(name)) {
name = `${baseName}${counter}`
counter++
}
existingNames.add(name)
return name
}