Skip to content

Commit b80f4c8

Browse files
committed
Handle ollama's deviation from the OpenAI tool streaming spec
1 parent 4bac76d commit b80f4c8

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

packages/mcp-client/src/McpClient.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,18 @@ export class McpClient {
160160
for (const toolCall of delta.tool_calls ?? []) {
161161
// aggregating chunks into an encoded arguments JSON object
162162
if (!finalToolCalls[toolCall.index]) {
163+
/// first chunk of the tool call
163164
finalToolCalls[toolCall.index] = toolCall;
164-
}
165-
if (finalToolCalls[toolCall.index].function.arguments === undefined) {
166-
finalToolCalls[toolCall.index].function.arguments = "";
167-
}
168-
if (toolCall.function.arguments) {
169-
finalToolCalls[toolCall.index].function.arguments += toolCall.function.arguments;
165+
166+
/// ensure .function.arguments is always a string
167+
if (finalToolCalls[toolCall.index].function.arguments === undefined) {
168+
finalToolCalls[toolCall.index].function.arguments = "";
169+
}
170+
} else {
171+
/// any subsequent chunk to the same tool call
172+
if (toolCall.function.arguments) {
173+
finalToolCalls[toolCall.index].function.arguments += toolCall.function.arguments;
174+
}
170175
}
171176
}
172177
if (opts.exitIfFirstChunkNoTool && numOfChunks <= 2 && Object.keys(finalToolCalls).length === 0) {

0 commit comments

Comments
 (0)