feat(mcp): chat-side tool routing core (wave 1)#61
Merged
Conversation
MacMLXCore foundation for MCP tool calling in chat (GUI wiring = wave 2). - ToolValueBridge: total, lossless MCP Value <-> JSONValue conversion + MCP Tool -> OpenAI function-spec; .data -> base64 (documented lossy edge) - GenerateRequest.tools ([JSONValue]?) injected into UserInput.tools; the ToolCallProcessor engages ONLY when the request carries tools, so all non-tool /v1/chat/completions + GUI chat keep byte-for-byte passthrough (makeToolProcessor gate) - Streaming tool-call detection in runLLMGeneration via upstream ToolCallProcessor (10 model formats auto-inferred at load); terminal chunk carries ToolCallRequest[] + finishReason .toolCalls; tool syntax stripped before reasoning-split, extended prompt-cache key unaffected - ChatMessage gains .tool role + toolCallID/toolCalls (all decodeIfPresent, legacy conversations still decode); mapped to upstream Chat.Message - ToolCallingSession: closure-injected generate + callTool agent loop — sequential tools, per-call cancellation-aware timeout, iteration cap, unknown-tool/throw/timeout fed back as error results, cancellation propagation. Value-type Sendable, no leaked tasks. - Server: incoming role:"tool" turns still dropped until wave 2 wires proper pairing (avoids a chat-template 500 on replayed tool history) 27 tests (value bridge, detection, session loop incl. deterministic cancellation, Codable back-compat, the two review-fix gates). Verified green under xcodebuild (Metal) + bare swift test.
|
Bugbot is not enabled for your account, so this pull request was not reviewed. Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
MacMLXCore foundation for MCP tool calling in chat — the v0.5 roadmap closer. GUI wiring (chat bubbles, AppState pool lifecycle, Integrations screen) is wave 2, following this.
What lands (tasks T1-T5)
Value⇄JSONValue+ MCPTool→ OpenAI function-spec;.data→base64 (documented lossy edge).GenerateRequest.toolsinjected intoUserInput.tools; the upstreamToolCallProcessor(10 model formats, auto-inferred at load) engages only when the request carries tools — all non-tool/v1/chat/completions+ GUI chat keep byte-for-byte passthrough.runLLMGeneration: terminal chunk carriesToolCallRequest[]+finishReason:.toolCalls; tool syntax stripped before reasoning-split; extended prompt-cache key unaffected.ChatMessagegains.toolrole +toolCallID/toolCalls(alldecodeIfPresent— legacy conversations still decode), mapped to upstreamChat.Message.ToolCallingSession— closure-injectedgenerate+callToolagent loop: sequential tools, per-call cancellation-aware timeout, iteration cap, unknown-tool/throw/timeout fed back as error results, cancellation propagation. Value-typeSendable, no leaked tasks.role:"tool"turns until wave 2 (avoids a chat-template 500 on replayed tool history).Review + verification
Adversarially reviewed (REQUEST_CHANGES → both blockers fixed): the HIGH
ToolCallProcessorunconditional-activation regression and the MEDIUM.tooldecode-leak are both fixed with dedicated tests. Loop state machine (exactly-once finish), timeout race, value bridge (int/double fidelity), Codable back-compat, and thestruct: Sendableconcurrency model all adjudicated sound; zero force-unwraps/try!/as!. 27 tests; verified green under both xcodebuild (Metal, 80 XCTest + DeepSeek parity) and bareswift test(236 tests, 0 MLX errors) — the two CI-equivalent conditions.