Skip to content

fix: update tool result content formatting in AnthropicAugmentedLLM #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

StreetLamb
Copy link
Collaborator

Fixes #94. Previously MCP server response from tool calling was not converted to Anthropic's ToolResultBlockParam content type.

@StreetLamb StreetLamb changed the title fix: update tool result content formatting in AnthropicAugmentedLLM WIP fix: update tool result content formatting in AnthropicAugmentedLLM Apr 1, 2025
@StreetLamb StreetLamb changed the title WIP fix: update tool result content formatting in AnthropicAugmentedLLM fix: update tool result content formatting in AnthropicAugmentedLLM Apr 1, 2025
Copy link
Collaborator

@saqadri saqadri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great fix, @StreetLamb! Wondering about the proper place for this, but otherwise lgtm

Comment on lines 215 to 248
# Convert mcp CallToolResult content to anthropic ToolResultBlockParam content
tool_result_content: list[
Union[TextBlockParam, ImageBlockParam]
] = []
for content_block in result.content:
if isinstance(content_block, TextContent):
tool_result_content.append(
TextBlockParam(type="text", text=content_block.text)
)
elif isinstance(content_block, ImageContent):
tool_result_content.append(
ImageBlockParam(
type="image",
source=Base64ImageSourceParam(
type="base64",
data=content_block.data,
media_type=content_block.mimeType,
),
)
)
elif isinstance(content_block, EmbeddedResource):
if isinstance(
content_block.resource, TextResourceContents
):
return TextBlockParam(
type="text", text=content_block.resource.text
)
else:
# Best effort to convert since this is not supported by anthropic ToolResultBlockParam.
return TextBlockParam(
type="text",
text=f"{content_block.resource.mimeType}:{content_block.resource.blob}",
)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this logic be in or utilize the AnthropicMCPTypeConverter class below?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was planning to leverage on from_mcp_message_param method, but it expects a MessageParam containing a single content block, whereas the call_tool returns a result with a list of content blocks so they are not really compatible.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we loop through the list of content blocks returned by the call_tool and use the from_mcp_message_param method?

Alternatively, we could add a from_mcp_tool_call method to the type converter. I feel like this would be a common enough issue across all model providers. But lmk what you think -- happy to merge in as is as well

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a new from_mcp_tool_result method to handle tool results from MCP servers. Let me know what you think!

Copy link
Collaborator

@saqadri saqadri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks! Do we need to update any other AugmentedLLM classes as well for this?

@saqadri saqadri merged commit 3142229 into lastmile-ai:main Apr 3, 2025
4 checks passed
saqadri pushed a commit that referenced this pull request Apr 17, 2025
)

* fix: update tool result content formatting in AnthropicAugmentedLLM

* fix: include type attribute in tool result content blocks

* Enhance tool result content conversion

* feat: add conversion method for MCP tool results to LLM input type

* feat: refactor tool result handling in AnthropicAugmentedLLM
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.

Image Tool Use Result Causes Response Error in Anthropic API
2 participants