π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
β¨ feat: make tool-name MD5 compression length configurable (#17249)
LiJian11842c86deed
diff --git a/packages/context-engine/src/engine/tools/ToolNameResolver.ts b/packages/context-engine/src/engine/tools/ToolNameResolver.tsindex 173c3c1e58c..93a3b29d6ca 100644--- a/packages/context-engine/src/engine/tools/ToolNameResolver.ts+++ b/packages/context-engine/src/engine/tools/ToolNameResolver.ts@@ -9,6 +9,48 @@ const PLUGIN_SCHEMA_SEPARATOR = '____';const PLUGIN_SCHEMA_API_MD5_PREFIX = 'MD5HASH_';const TOOL_NAME_COMPONENT_PATTERN = /^[\w-]+$/;+// OpenAI GPT function_call names can't be longer than 64 characters, so long+// names are compressed to an MD5 hash. Other providers don't have this limit,+// and the opaque hash hurts readability, so the threshold is configurable via+// the `TOOL_NAME_MAX_LENGTH` env var (`0` disables length-based compression).+const DEFAULT_TOOL_NAME_MAX_LENGTH = 64;++/**+ * Read the threshold from env, defaulting to 64. Read directly (not through the+ * app env layer) and at module load so it is correct on every serverless worker+ * / cold start β the same name must compress identically wherever `generate()`+ * and `resolve()` run for an operation, including resume paths that never touch+ * the tool-engine setup. Guarded for non-Node runtimes (browser SPA) where+ * `process` may be undefined; there it falls back to the default.+ */+const readEnvMaxLength = (): number => {+ try {+ const raw = typeof process === 'undefined' ? undefined : process.env?.TOOL_NAME_MAX_LENGTH;+ if (raw === undefined || raw === '') return DEFAULT_TOOL_NAME_MAX_LENGTH;+ const parsed = Number.parseInt(raw, 10);+ return Number.isFinite(parsed) && parsed >= 0 ? parsed : DEFAULT_TOOL_NAME_MAX_LENGTH;+ } catch {+ return DEFAULT_TOOL_NAME_MAX_LENGTH;+ }+};++let toolNameMaxLength = readEnvMaxLength();++/**+ * Override the max tool-name length before MD5 compression kicks in. Mainly for+ * tests and hosts that source the value differently; normal runtime picks it up+ * from env at module load. Pass `0` (or negative) to disable length-based+ * compression entirely; `undefined`/non-finite re-reads the env default.+ * Invalid-character normalization is independent and always applies.+ */+export const setToolNameMaxLength = (value: number | undefined): void => {+ toolNameMaxLength =+ typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : readEnvMaxLength();+};++/** Current max tool-name length; `0` means length-based compression is off. */+export const getToolNameMaxLength = (): number => toolNameMaxLength;+/*** Tool Name Resolver* Handles tool name generation and resolution for function calling@@ -59,14 +101,19 @@ export class ToolNameResolver {// Step 1: Try normal formatlet toolName = identifierName + PLUGIN_SCHEMA_SEPARATOR + apiName + pluginType;- // OpenAI GPT function_call name can't be longer than 64 characters- // Step 2: If >= 64, hash the name part- if (toolName.length >= 64) {+ // Length-based MD5 compression. Gated on the configured max length so it can+ // be tuned per deployment (or disabled with 0) β only providers that cap+ // function names (e.g. OpenAI at 64) actually need it, and the hash hurts+ // readability. `0`/negative disables it entirely. Invalid-character+ // normalization above is independent and always applies.+ const maxLength = getToolNameMaxLength();+ // Step 2: If >= maxLength, hash the name part+ if (maxLength > 0 && toolName.length >= maxLength) {apiName = this.hashComponent(name);toolName = identifierName + PLUGIN_SCHEMA_SEPARATOR + apiName + pluginType;- // Step 3: If still >= 64, also hash the identifier- if (toolName.length >= 64) {+ // Step 3: If still >= maxLength, also hash the identifier+ if (toolName.length >= maxLength) {identifierName = this.hashComponent(identifier);toolName = identifierName + PLUGIN_SCHEMA_SEPARATOR + apiName + pluginType;}diff --git a/packages/context-engine/src/engine/tools/__tests__/ToolNameResolver.config.test.ts b/packages/context-engine/src/engine/tools/__tests__/ToolNameResolver.config.test.tsnew file mode 100644index 00000000000..0d2d81e0f0a--- /dev/null+++ b/packages/context-engine/src/engine/tools/__tests__/ToolNameResolver.config.test.ts@@ -0,0 +1,87 @@+import { afterEach, describe, expect, it } from 'vitest';++import { getToolNameMaxLength, setToolNameMaxLength, ToolNameResolver } from '../ToolNameResolver';++const resolver = new ToolNameResolver();++// A name whose `identifier____apiName` comfortably exceeds 64 chars.+const longIdentifier = 'a-fairly-long-connector-identifier-value';+const longApiName = 'someActionNameThatIsAlsoQuiteLongForGoodMeasureAndThenSome';++afterEach(() => {+ // Reset to the default so a stray config can't leak into other cases.+ setToolNameMaxLength(undefined);+});++describe('configurable tool-name max length', () => {+ it('defaults to 64 and compresses long names (unchanged behavior)', () => {+ expect(getToolNameMaxLength()).toBe(64);+ const result = resolver.generate(longIdentifier, longApiName, 'mcp');+ expect(result.length).toBeLessThan(64);+ expect(result).toContain('MD5HASH_');+ });++ it('disables length compression when set to 0', () => {+ setToolNameMaxLength(0);+ const result = resolver.generate(longIdentifier, longApiName, 'mcp');+ // Full, readable name β no MD5 hash, even though it exceeds 64 chars.+ expect(result).toBe(`${longIdentifier}____${longApiName}____mcp`);+ expect(result).not.toContain('MD5HASH_');+ expect(result.length).toBeGreaterThan(64);+ });++ it('still normalizes provider-invalid characters when compression is disabled', () => {+ setToolNameMaxLength(0);+ // Length compression is off, but invalid characters must still be hashed so+ // the wire name stays provider-safe β that is independent of length.+ const result = resolver.generate('mcp-server', 'get.current/weather', 'mcp');+ expect(result).toMatch(/^mcp-server____MD5HASH_[\da-f]+____mcp$/);+ expect(result).toMatch(/^[\w-]+$/);+ expect(result).not.toContain('get.current/weather');+ });++ it('compresses at a custom lower threshold', () => {+ // A ~48-char name: under the default 64 (stays readable) but over 30.+ const identifier = 'plugin';+ const apiName = 'anActionNameOverThirtyCharsLong';++ // Default threshold: not compressed.+ expect(resolver.generate(identifier, apiName, 'mcp')).not.toContain('MD5HASH_');++ // Lower the threshold to 30: the same name now compresses.+ setToolNameMaxLength(30);+ expect(resolver.generate(identifier, apiName, 'mcp')).toContain('MD5HASH_');+ // Names comfortably under the threshold stay untouched.+ expect(resolver.generate('plugin', 'action', 'mcp')).toBe('plugin____action____mcp');+ });++ it('resets to the default for invalid/undefined input', () => {+ setToolNameMaxLength(20);+ expect(getToolNameMaxLength()).toBe(20);+ setToolNameMaxLength(undefined);+ expect(getToolNameMaxLength()).toBe(64);+ setToolNameMaxLength(Number.NaN);+ expect(getToolNameMaxLength()).toBe(64);+ setToolNameMaxLength(-5);+ expect(getToolNameMaxLength()).toBe(64);+ });++ it('still roundtrips through resolve when compression is disabled', () => {+ setToolNameMaxLength(0);+ const toolName = resolver.generate(longIdentifier, longApiName, 'mcp');+ const [resolved] = resolver.resolve(+ [{ function: { arguments: '{}', name: toolName }, id: 'call_1', type: 'function' }],+ {+ [longIdentifier]: {+ api: [{ description: '', name: longApiName, parameters: {} }],+ identifier: longIdentifier,+ meta: {},+ type: 'mcp' as any,+ },+ },+ );+ expect(resolved.identifier).toBe(longIdentifier);+ expect(resolved.apiName).toBe(longApiName);+ expect(resolved.type).toBe('mcp');+ });+});diff --git a/packages/context-engine/src/engine/tools/index.ts b/packages/context-engine/src/engine/tools/index.tsindex e3dc3eb33a6..45cbbbec1e7 100644--- a/packages/context-engine/src/engine/tools/index.ts+++ b/packages/context-engine/src/engine/tools/index.ts@@ -2,7 +2,7 @@export { ToolsEngine } from './ToolsEngine';// Tool Name Resolver-export { ToolNameResolver } from './ToolNameResolver';+export { getToolNameMaxLength, setToolNameMaxLength, ToolNameResolver } from './ToolNameResolver';// Tool Arguments Repairerexport { ToolArgumentsRepairer, type ToolParameterSchema } from './ToolArgumentsRepairer';