This repository was archived by the owner on May 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathmcp-name.ts
More file actions
125 lines (105 loc) · 3.95 KB
/
Copy pathmcp-name.ts
File metadata and controls
125 lines (105 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* Utilities for sanitizing MCP server and tool names to conform to
* API function name requirements across all providers.
*/
/**
* Separator used between MCP prefix, server name, and tool name.
* We use "--" (double hyphen) because:
* 1. It's allowed by all providers (dashes are permitted in function names)
* 2. It won't conflict with underscores in sanitized server/tool names
* 3. It's unique enough to be a reliable delimiter for parsing
*/
export const MCP_TOOL_SEPARATOR = "--"
/**
* Prefix for all MCP tool function names.
*/
export const MCP_TOOL_PREFIX = "mcp"
/**
* Check if a tool name is an MCP tool (starts with the MCP prefix and separator).
*
* @param toolName - The tool name to check
* @returns true if the tool name starts with "mcp--", false otherwise
*/
export function isMcpTool(toolName: string): boolean {
return toolName.startsWith(`${MCP_TOOL_PREFIX}${MCP_TOOL_SEPARATOR}`)
}
/**
* Sanitize a name to be safe for use in API function names.
* This removes special characters and ensures the name starts correctly.
*
* Note: This does NOT remove dashes from names, but the separator "--" is
* distinct enough (double hyphen) that single hyphens in names won't conflict.
*
* @param name - The original name (e.g., MCP server name or tool name)
* @returns A sanitized name that conforms to API requirements
*/
export function sanitizeMcpName(name: string): string {
if (!name) {
return "_"
}
// Replace spaces with underscores first
let sanitized = name.replace(/\s+/g, "_")
// Only allow alphanumeric, underscores, and dashes
sanitized = sanitized.replace(/[^a-zA-Z0-9_\-]/g, "")
// Replace any double-hyphen sequences with single hyphen to avoid separator conflicts
sanitized = sanitized.replace(/--+/g, "-")
// Ensure the name starts with a letter or underscore
if (sanitized.length > 0 && !/^[a-zA-Z_]/.test(sanitized)) {
sanitized = "_" + sanitized
}
// If empty after sanitization, use a placeholder
if (!sanitized) {
sanitized = "_unnamed"
}
return sanitized
}
/**
* Build a full MCP tool function name from server and tool names.
* The format is: mcp--{sanitized_server_name}--{sanitized_tool_name}
*
* The total length is capped at 64 characters to conform to API limits.
*
* @param serverName - The MCP server name
* @param toolName - The tool name
* @returns A sanitized function name in the format mcp--serverName--toolName
*/
export function buildMcpToolName(serverName: string, toolName: string): string {
const sanitizedServer = sanitizeMcpName(serverName)
const sanitizedTool = sanitizeMcpName(toolName)
// Build the full name: mcp--{server}--{tool}
const fullName = `${MCP_TOOL_PREFIX}${MCP_TOOL_SEPARATOR}${sanitizedServer}${MCP_TOOL_SEPARATOR}${sanitizedTool}`
// Truncate if necessary (max 64 chars for Gemini)
if (fullName.length > 64) {
return fullName.slice(0, 64)
}
return fullName
}
/**
* Parse an MCP tool function name back into server and tool names.
* This handles sanitized names by splitting on the "--" separator.
*
* Note: This returns the sanitized names, not the original names.
* The original names cannot be recovered from the sanitized version.
*
* @param mcpToolName - The full MCP tool name (e.g., "mcp--weather--get_forecast")
* @returns An object with serverName and toolName, or null if parsing fails
*/
export function parseMcpToolName(mcpToolName: string): { serverName: string; toolName: string } | null {
const prefix = MCP_TOOL_PREFIX + MCP_TOOL_SEPARATOR
if (!mcpToolName.startsWith(prefix)) {
return null
}
// Remove the "mcp--" prefix
const remainder = mcpToolName.slice(prefix.length)
// Split on the separator to get server and tool names
const separatorIndex = remainder.indexOf(MCP_TOOL_SEPARATOR)
if (separatorIndex === -1) {
return null
}
const serverName = remainder.slice(0, separatorIndex)
const toolName = remainder.slice(separatorIndex + MCP_TOOL_SEPARATOR.length)
if (!serverName || !toolName) {
return null
}
return { serverName, toolName }
}