Skip to content

fix(gateway): truncate long tool names instead of rejecting them#104

Open
juslintek wants to merge 3 commits intojwadow:mainfrom
juslintek:fix/truncate-long-tool-names
Open

fix(gateway): truncate long tool names instead of rejecting them#104
juslintek wants to merge 3 commits intojwadow:mainfrom
juslintek:fix/truncate-long-tool-names

Conversation

@juslintek
Copy link
Copy Markdown

Problem

MCP plugins generate tool names that exceed the 64-character Kiro API limit, causing a 400 error:

Tool name(s) exceed Kiro API limit of 64 characters:
  - 'mcp__plugin_cloudflare_cloudflare-docs__search_cloudflare_documentation' (71 chars)
  - 'mcp__plugin_deploy-on-aws_awsiac__get_cloudformation_pre_deploy_validation_instructions' (87 chars)

This blocks all requests when any MCP plugin has long auto-generated names.

Solution

Replace validate_tool_names (which rejected with 400) with truncate_tool_names that deterministically shortens names exceeding 64 chars to <55 chars>_<8-char md5 hash> = 64 chars exactly.

The mapping is reversed in responses so clients receive the original tool names and can execute tools correctly.

How it works

  1. Request path: tool definitions and tool_use blocks in history are truncated before sending to Kiro API
  2. Response path: tool names in tool_use events are mapped back to originals before returning to client
  3. Deterministic: same input always produces same short name (no cross-request state needed)

Example

mcp__plugin_cloudflare_cloudflare-docs__search_cloudflare_documentation (71)
→ mcp__plugin_cloudflare_cloudflare-docs__search_cloudfla_d29d0e79 (64)

Depends on

PancakeZik and others added 3 commits March 3, 2026 18:18
The prompt_tokens reported to clients (used by Claude Code's /context
command) were wildly inaccurate because they were derived from Kiro's
contextUsagePercentage, which returns unreliable values.

Instead, count tokens from the complete serialized Kiro request payload
using tiktoken. This includes system prompt, messages, tools, and all
other payload fields — matching what actually gets sent to the API.

- Replace request_messages/request_tools params with pre-counted
  prompt_tokens across all streaming functions
- Count tokens from full kiro_request_body in both OpenAI and
  Anthropic route handlers
- Remove dependency on contextUsagePercentage for token counting
- Update tests to match new function signatures
Claude Code calls this endpoint before each request to check conversation
size and decide whether to trigger compaction. Without it, the gateway
returns 404, Claude Code cannot estimate context usage, and long
conversations eventually hit the upstream CONTENT_LENGTH_EXCEEDS_THRESHOLD
error (400).

The endpoint builds the full Kiro payload and counts tokens on the
serialized JSON using tiktoken, consistent with the token counting
approach used in the messages endpoint.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace validate_tool_names (400 error) with deterministic truncation
for tool names exceeding 64-char Kiro API limit. Names are shortened
to 55 chars + '_' + 8-char md5 hash. Mapping is reversed in responses
so clients receive original names.

Fixes MCP plugins with auto-generated names like
mcp__plugin_cloudflare_cloudflare-docs__search_cloudflare_documentation
@cla-bot
Copy link
Copy Markdown

cla-bot bot commented Mar 20, 2026

Thanks for the PR! 🎉

Before merge, we need a one-time CLA confirmation.
It confirms that you have the right to contribute this code and allow the project to use it.

Full CLA text:
https://github.com/jwadow/kiro-gateway/blob/main/CLA.md

Please reply once with:

I have read the CLA and I accept its terms

You need to write once, all further messages from me can be ignored.

@juslintek
Copy link
Copy Markdown
Author

I have read the CLA and I accept its terms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants