Skip to content

fix(bedrock): sanitize tool names for Bedrock API constraints #1473

@frank-bee

Description

@frank-bee

Problem

When using kagent with Amazon Bedrock models, MCP servers (e.g. GitHub Copilot) may produce tool names containing dots, spaces, or other characters that violate Bedrock's API constraints.

Bedrock requires tool names to match [a-zA-Z0-9_-]+ with a minimum length of 1. Any name outside this pattern causes API errors like:

ValidationException: A tool name is invalid. Tool names can only contain letters, numbers, underscores, and hyphens

Root Cause

KAgentLiteLlm does not sanitize tool names before passing them to the Bedrock API, and does not sanitize tool names in the conversation history (function call parts) sent in subsequent requests.

Fix

Override generate_content_async in KAgentLiteLlm to:

  • Sanitize tool names in the outgoing LlmRequest history (function call parts in contents)
  • Sanitize tool names in incoming LlmResponse parts

Invalid characters are replaced with _. Empty/completely invalid names fall back to unknown_tool_<idx>. A warning is logged whenever a name is sanitized, to aid debugging.

Affected File

python/packages/kagent-adk/src/kagent/adk/models/_litellm.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions