fix: remove dots and colons from MCP tool names for Bedrock compatibility (#10152)

This commit is contained in:
Daniel 2025-12-17 11:47:52 -05:00 • committed by GitHub
parent 4693b9eb06
commit ed631dccc3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 13 deletions

View file

@ -23,18 +23,24 @@ describe("mcp-name utilities", () => {
expect(sanitizeMcpName("test#$%^&*()")).toBe("test")
})
it("should keep valid characters (alphanumeric, underscore, dot, colon, dash)", () => {
it("should keep valid characters (alphanumeric, underscore, dash)", () => {
expect(sanitizeMcpName("server_name")).toBe("server_name")
expect(sanitizeMcpName("server.name")).toBe("server.name")
expect(sanitizeMcpName("server:name")).toBe("server:name")
expect(sanitizeMcpName("server-name")).toBe("server-name")
expect(sanitizeMcpName("Server123")).toBe("Server123")
})
it("should remove dots and colons for AWS Bedrock compatibility", () => {
// Dots and colons are NOT allowed due to AWS Bedrock restrictions
expect(sanitizeMcpName("server.name")).toBe("servername")
expect(sanitizeMcpName("server:name")).toBe("servername")
expect(sanitizeMcpName("awslabs.aws-documentation-mcp-server")).toBe("awslabsaws-documentation-mcp-server")
})
it("should prepend underscore if name starts with non-letter/underscore", () => {
expect(sanitizeMcpName("123server")).toBe("_123server")
expect(sanitizeMcpName("-server")).toBe("_-server")
expect(sanitizeMcpName(".server")).toBe("_.server")
// Dots are removed, so ".server" becomes "server" which starts with a letter
expect(sanitizeMcpName(".server")).toBe("server")
})
it("should not modify names that start with letter or underscore", () => {

View file

@ -1,17 +1,12 @@
/**
* Utilities for sanitizing MCP server and tool names to conform to
* API function name requirements (e.g., Gemini's restrictions).
*
* Gemini function name requirements:
* - Must start with a letter or an underscore
* - Must be alphanumeric (a-z, A-Z, 0-9), underscores (_), dots (.), colons (:), or dashes (-)
* - Maximum length of 64 characters
* 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 Gemini (dashes are permitted in function names)
* 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
*/
@ -40,8 +35,8 @@ export function sanitizeMcpName(name: string): string {
// Replace spaces with underscores first
let sanitized = name.replace(/\s+/g, "_")
// Remove any characters that are not alphanumeric, underscores, dots, colons, or dashes
sanitized = sanitized.replace(/[^a-zA-Z0-9_.\-:]/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, "-")