Repository navigation
refactor(mcp): extract explicit operation context and dispatch - #42292
Conversation
|
bugbot run. Please review e208b4e for explicit caller isolation, legacy callback compatibility, toolset authorization, cleanup, and the approved operation boundary |
|
@veria-ai Please review e208b4e for explicit caller isolation, legacy callback compatibility, toolset authorization, cleanup, and the approved operation boundary |
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
@greptileai Please review 9002749: Python 3.10 typing imports and older integration mocks are corrected; inspect explicit context and toolset authorization |
|
bugbot run. Please review 9002749. The toolset finding is under independent ingress verification, including missing credentials and excluded upstream side effects |
|
@veria-ai Please review 9002749 for explicit caller isolation and toolset authorization; CI fixes restore Python 3.10 imports and integration mocks |
|
bugbot run. Please reassess the existing toolset finding using the independent baseline/current ingress replay and zero-upstream-contact evidence in its thread |
|
@greptileai Please review ef67412: OAuth prefetch warnings omit caller/error data, with a failing-before newline regression; static export/return notes clarified |
|
@veria-ai Please review ef67412: OAuth prefetch warnings omit caller/error data, with a failing-before newline regression; static export/return notes clarified |
|
bugbot run. Please review ef67412 and reassess the toolset concern using the thread evidence: denied ingress contacts no upstream server |
|
CodeQL hashing alerts are documented false positives: unchanged in-memory session-owner fingerprints, never password verifiers. Independent data-flow review confirmed the disposition |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit ef67412. Configure here.
TLDR
Problem this solves:
How it solves it:
This implements the LIT-7741 operation/context boundary. Most of
operations.pyis existing policy moved fromserver.py; native, REST and virtual-tool consumers reuse it.OperationContextsnapshots caller data without dropping trusted server-only fields, andGatewayOperations.executedispatches the seven supported SDK operations. Boundary lint prevents shared operations from importing legacy request/session stateSampling previously read ambient headers when invoked, losing explicit REST headers. Callbacks now capture their initiating context, including the legacy explicit-caller entry point. Elicitation retains its existing injected session contract. Toolset scope travels as a server-only marker so an operator-open server cannot bypass the selected toolset
User Flow
Before: a REST tool call's sampling completion loses its request tag
GET http://localhost:4000/mcp-rest/tools/listwith an authorized key and see the probe toolPOST http://localhost:4000/mcp-rest/tools/callwith that tool, a caller label, and anx-litellm-tagsrequest tag; receive a successful sampled responserequest_tags: []After: the sampling completion retains the same caller's tag
GET http://localhost:4000/mcp-rest/tools/listwith an authorized key and see the probe toolPOST http://localhost:4000/mcp-rest/tools/callwith that tool, a caller label, and anx-litellm-tagsrequest tag; receive a successful sampled responseRelevant issues
Related open PR #37625 covers prompt/resource OAuth identity forwarding, a small overlap with explicit context propagation here. It does not implement this ticket's operation boundary; this PR makes no supersession claim
Affected release
Linear ticket
Resolves LIT-7741
Pre-Submission checklist
Local validation at
ef67412e50c9f3250eb473a0a14fbf00617e36ea: 1,651 affected tests passed with four preexisting skips; a separate older integration run passed 53 tests with one preexisting skip. Full Ruff and test-tree lint, formatting, strict-rule, type-discipline, test-quality, canonical basedpyright budget, boundary lint, circular imports and schema generation passed. No budgets or coverage exclusions changedLocal changed executable-line coverage: 1,009/1,052 (95.91%). Touched-function branch coverage: 539/628 (85.83%). Remaining gaps are primarily relocated legacy policy fallbacks; new context/callback modules have full changed-line coverage. Completed hosted Codecov: all 27 uploads merged, repository coverage 76.05%, patch 85/85 (100%). Codecov excludes relocated bodies from its patch count; the local 95.91% figure above measures all changed executable lines. The final per-file report includes all eight changed production files; its remaining gaps are within the documented legacy fallbacks
Independent Devin verification, through the configured MCP integration, passed at the same commit: 1,329 mapped tests and 53 older integration tests, boundary lint, live native/REST sampling with real OpenAI calls, concurrent caller isolation, and allowed/denied toolset calls including an operator-open server. Its explicit-caller fallback finding was reproduced, fixed, and independently confirmed resolved. The current-tip log-injection probe confirms that failed OAuth prefetch logs contain neither caller text nor exception text; denied toolset ingress produced zero upstream requests
Current-tip reviews: Greptile 5/5, Veria passed, and Bugbot found no issues and resolved its earlier toolset concern after independent ingress evidence. Contributor agreement confirmed signed by the author. All required CI and the additional OAuth/security jobs pass; hosted coverage is complete
Delays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review)
Screenshots / Proof of Fix
Latest run, 2026-09-21. Two live source builds side by side, each started with
--num_workers 2from its own checkout on a random port (Before36b8be7d81b414f4866e767a7e00ff84a7fd865eon 16584, Afteref67412e50c9f3250eb473a0a14fbf00617e36eaon 16585), each with its own freshly migrated PostgreSQL database, its own controlled MCP upstream with sampling, and its own forwarding recorder in front ofhttps://api.openai.comset as the model'sapi_baseso the paidopenai/gpt-5.4-minirequest bodies could be diffed between sides. Readiness on both sides returned{"status":"healthy","db":"connected"}before the first step. The same steps were then rerun a third time on the merged tree (origin/mainat1baa26d36bde2eff34c773b4f7aad7d3b0369c64, which contains this PR's merge commit) rebooted on the After ports with a fresh databaseShared setup and visible upstream implementation
Use MCP SDK 2.2.0 and a migrated PostgreSQL database per side. Supply
OPENAI_API_KEY,LITELLM_MASTER_KEY, andDATABASE_URLprivately.RECis the port of a small local forwarder that logs each request and proxies it tohttps://api.openai.com; pointapi_basestraight at OpenAI if you do not need the wire diff.UPis the upstream portThe controlled upstream executes a real MCP sampling request and also exposes an
echotool, a prompt, and two resourcesStart the upstream on
UPand the selected source checkout on its portPXwith two workers, then create the caller's virtual key (kept privately asBRAVO) and read the server idRUNbelow isbase-r2for Before,head-r2for After, andmerged-r3for the merged tree, so every key alias, tag, and caller label names the side it ran onBefore (36b8be7, port 16584)
List tools with the caller's key
Call the sampling tool with a request tag
Read the caller's rows through the spend-logs API
The tool row carries the tag; the sampling
acompletionrow under the same key alias hasrequest_tags: []After (ef67412, port 16585)
Same list command against port 16585: HTTP 200, the same two tools,
server_outcomes.probe_srv.tool_count2Same call command against port 16585 with
lane-rest-bravo-head-r2and callerbravo-head-r2Same spend-logs read against port 16585 filtered on
prrisk-bravo-head-r2Both rows carry the caller's tag under the same key alias
Merged tree (1baa26d on port 16585, fresh database)
The same three steps with
merged-r3labels returned HTTP 200, the same tools, the same sampled reply, and tagged rows at23:16:07.397(call_mcp_tool) and23:16:07.711(acompletion), both["lane-rest-bravo-merged-r3","User-Agent: curl","User-Agent: curl/8.7.1"]Every other surface in the same run, identical on all three trees
Each run also drove native
/mcpand/mcp/proxysessions (initialize, list, call, prompt, resource, template),/v1/responseswith alitellm_proxyMCP tool,/v1/mcp/tools,/mcp-rest/tools/listwith no key and with a bad key, empty, 5 KB, and duplicatex-litellm-tagsheaders, two concurrent callers with different keys, a toolset with/toolset/{name}/mcpfor a granted and a non-granted key, and the toolset REST list. HTTP statuses, REST bodies, native outcomes, server and toolset listings, and the Responses output types matched across Before, After, and the merged tree modulo ids and timestamps. Each run wrote 24 aliased spend rows; the five samplingacompletionrows carry their lane tag with no cross-caller mixing on After and on the merged tree, and on Before only the native/mcpleg's sampling row is tagged. The OpenAI request bodies captured by the recorders were identical between sideslist_toolsempty, calls denied, both sides/toolset/{name}/mcpneeds the toolset id inmcp_toolsets, else 403/mcpleg's sampling rowType
Refactoring, Bug Fix
Caveats (if any)
Low
Final Attestation