-
Notifications
You must be signed in to change notification settings - Fork 3
fix: only disable parallel tool use when tools are injected #96
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| package aibridge | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/anthropics/anthropic-sdk-go" | ||
| "github.com/anthropics/anthropic-sdk-go/shared/constant" | ||
| "github.com/coder/aibridge/mcp" | ||
| mcpgo "github.com/mark3labs/mcp-go/mcp" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestInjectTools_ParallelToolCalls(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("does not modify tool choice when no tools to inject", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| i := &AnthropicMessagesInterceptionBase{ | ||
| req: &MessageNewParamsWrapper{ | ||
| MessageNewParams: anthropic.MessageNewParams{ | ||
| ToolChoice: anthropic.ToolChoiceUnionParam{ | ||
| OfAuto: &anthropic.ToolChoiceAutoParam{ | ||
| Type: constant.ValueOf[constant.Auto](), | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| mcpProxy: &mockServerProxier{tools: nil}, // No tools to inject. | ||
| } | ||
|
|
||
| i.injectTools() | ||
|
|
||
| // Tool choice should remain unchanged - DisableParallelToolUse should not be set. | ||
| require.NotNil(t, i.req.ToolChoice.OfAuto) | ||
| require.False(t, i.req.ToolChoice.OfAuto.DisableParallelToolUse.Valid()) | ||
| }) | ||
|
|
||
| t.Run("disables parallel tool use for auto tool choice (default)", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| i := &AnthropicMessagesInterceptionBase{ | ||
| req: &MessageNewParamsWrapper{ | ||
| MessageNewParams: anthropic.MessageNewParams{ | ||
| // No tool choice set (default). | ||
| }, | ||
| }, | ||
| mcpProxy: &mockServerProxier{ | ||
| tools: []*mcp.Tool{{ID: "test_tool", Name: "test", Description: "Test"}}, | ||
| }, | ||
| } | ||
|
|
||
| i.injectTools() | ||
|
|
||
| require.NotNil(t, i.req.ToolChoice.OfAuto) | ||
| require.True(t, i.req.ToolChoice.OfAuto.DisableParallelToolUse.Valid()) | ||
| require.True(t, i.req.ToolChoice.OfAuto.DisableParallelToolUse.Value) | ||
| }) | ||
|
|
||
| t.Run("disables parallel tool use for explicit auto tool choice", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| i := &AnthropicMessagesInterceptionBase{ | ||
| req: &MessageNewParamsWrapper{ | ||
| MessageNewParams: anthropic.MessageNewParams{ | ||
| ToolChoice: anthropic.ToolChoiceUnionParam{ | ||
| OfAuto: &anthropic.ToolChoiceAutoParam{ | ||
| Type: constant.ValueOf[constant.Auto](), | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| mcpProxy: &mockServerProxier{ | ||
| tools: []*mcp.Tool{{ID: "test_tool", Name: "test", Description: "Test"}}, | ||
| }, | ||
| } | ||
|
|
||
| i.injectTools() | ||
|
|
||
| require.NotNil(t, i.req.ToolChoice.OfAuto) | ||
| require.True(t, i.req.ToolChoice.OfAuto.DisableParallelToolUse.Valid()) | ||
| require.True(t, i.req.ToolChoice.OfAuto.DisableParallelToolUse.Value) | ||
| }) | ||
|
|
||
| t.Run("disables parallel tool use for any tool choice", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| i := &AnthropicMessagesInterceptionBase{ | ||
| req: &MessageNewParamsWrapper{ | ||
| MessageNewParams: anthropic.MessageNewParams{ | ||
| ToolChoice: anthropic.ToolChoiceUnionParam{ | ||
| OfAny: &anthropic.ToolChoiceAnyParam{ | ||
| Type: constant.ValueOf[constant.Any](), | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| mcpProxy: &mockServerProxier{ | ||
| tools: []*mcp.Tool{{ID: "test_tool", Name: "test", Description: "Test"}}, | ||
| }, | ||
| } | ||
|
|
||
| i.injectTools() | ||
|
|
||
| require.NotNil(t, i.req.ToolChoice.OfAny) | ||
| require.True(t, i.req.ToolChoice.OfAny.DisableParallelToolUse.Valid()) | ||
| require.True(t, i.req.ToolChoice.OfAny.DisableParallelToolUse.Value) | ||
| }) | ||
|
|
||
| t.Run("disables parallel tool use for tool choice type", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| i := &AnthropicMessagesInterceptionBase{ | ||
| req: &MessageNewParamsWrapper{ | ||
| MessageNewParams: anthropic.MessageNewParams{ | ||
| ToolChoice: anthropic.ToolChoiceUnionParam{ | ||
| OfTool: &anthropic.ToolChoiceToolParam{ | ||
| Type: constant.ValueOf[constant.Tool](), | ||
| Name: "specific_tool", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| mcpProxy: &mockServerProxier{ | ||
| tools: []*mcp.Tool{{ID: "test_tool", Name: "test", Description: "Test"}}, | ||
| }, | ||
| } | ||
|
|
||
| i.injectTools() | ||
|
|
||
| require.NotNil(t, i.req.ToolChoice.OfTool) | ||
| require.True(t, i.req.ToolChoice.OfTool.DisableParallelToolUse.Valid()) | ||
| require.True(t, i.req.ToolChoice.OfTool.DisableParallelToolUse.Value) | ||
| }) | ||
|
|
||
| t.Run("no-op for none tool choice type", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| i := &AnthropicMessagesInterceptionBase{ | ||
| req: &MessageNewParamsWrapper{ | ||
| MessageNewParams: anthropic.MessageNewParams{ | ||
| ToolChoice: anthropic.ToolChoiceUnionParam{ | ||
| OfNone: &anthropic.ToolChoiceNoneParam{ | ||
| Type: constant.ValueOf[constant.None](), | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| mcpProxy: &mockServerProxier{ | ||
| tools: []*mcp.Tool{{ID: "test_tool", Name: "test", Description: "Test"}}, | ||
| }, | ||
| } | ||
|
|
||
| i.injectTools() | ||
|
|
||
| // Tools are still injected. | ||
| require.Len(t, i.req.Tools, 1) | ||
| // But no parallel tool use modification for "none" type. | ||
| require.Nil(t, i.req.ToolChoice.OfAuto) | ||
| require.Nil(t, i.req.ToolChoice.OfAny) | ||
dannykopping marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| require.Nil(t, i.req.ToolChoice.OfTool) | ||
| require.NotNil(t, i.req.ToolChoice.OfNone) | ||
| }) | ||
| } | ||
|
|
||
| // mockServerProxier is a test implementation of mcp.ServerProxier. | ||
| type mockServerProxier struct { | ||
| tools []*mcp.Tool | ||
| } | ||
|
|
||
| func (m *mockServerProxier) Init(context.Context) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (m *mockServerProxier) Shutdown(context.Context) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (m *mockServerProxier) ListTools() []*mcp.Tool { | ||
| return m.tools | ||
| } | ||
|
|
||
| func (m *mockServerProxier) GetTool(id string) *mcp.Tool { | ||
| for _, t := range m.tools { | ||
| if t.ID == id { | ||
| return t | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (m *mockServerProxier) CallTool(context.Context, string, any) (*mcpgo.CallToolResult, error) { | ||
| return nil, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I fully understand this case. The AI client sends a request with
tool_choice: none, telling Anthropic to not execute any tools. But AIBridge still injects the tools even though Anthropic will never execute them, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question! We don't conditionally inject tools based on tool choice right now. Maybe we should...
I'm in favour of not trying to be too clever / efficient here because this behaviour might confuse us / an operator looking at request logs. It might not be immediately apparently that tools didn't inject for this reason.