chore: [mirror of #9267] pass OAuthClientProvider to call_mcp_tool for automatic token refresh - #9414
Conversation
🖼️ Visual Regression Report
|
Greptile SummaryThis PR propagates Key observations:
Confidence Score: 4/5
Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[MCPTool.run called] --> B[Build headers from connection_config]
B --> C{has_auth_config?}
C -- No --> D[Return auth-required error]
C -- Yes --> E{auth_type == OAUTH?}
E -- No --> F[call_mcp_tool with headers only]
E -- Yes --> G{transport == SSE?}
G -- Yes --> H[Log warning, skip auth provider]
H --> F
G -- No --> I[make_oauth_provider with user_id and connection_config]
I --> J[call_mcp_tool with auth provider]
J --> K{Token valid?}
K -- Yes --> L[Return tool result]
K -- Expired, refresh available --> M[SDK refreshes token, retries]
M --> L
K -- Re-auth needed --> N[ValueError caught as auth error]
N --> O[Return reconnect message to user]
Prompt To Fix All With AIThis is a comment left during a code review.
Path: backend/onyx/tools/tool_implementations/mcp/mcp_tool.py
Line: 220-221
Comment:
**Deferred imports inside hot path**
`make_oauth_provider` and `UNUSED_RETURN_PATH` are imported inside `run()` on every OAUTH tool invocation. Python's module cache means this is safe (no repeated I/O), but deferred inline imports are a code smell that usually signals a circular-import problem. The comment in the code doesn't explain *why* the circular import exists; it only describes what the parameters are.
If the circular import can't be broken, at minimum a comment should explain the dependency cycle. If it *can* be broken (e.g., by moving `make_oauth_provider` to a lower-level module that `mcp_tool.py` doesn't transitively pull from), that would be preferred — the best-practices guide flags hidden import-time coupling as an issue to avoid.
How can I resolve this? If you propose a fix, please make it concise.
---
This is a comment left during a code review.
Path: backend/onyx/tools/tool_implementations/mcp/mcp_tool.py
Line: 205-232
Comment:
**Missing test coverage for the new auth provider path**
The new OAuth-provider branch (lines 205–232) is the primary new behavior in this PR but has no dedicated test. The existing test class only covers the pass-through OAuth flow. The SSE warning path, token-refresh happy path, and the reconnect-error path are all untested.
Consider adding tests that verify:
- An OAuth-type server with a valid connection config causes the auth provider to be non-None
- The SSE transport path results in auth being skipped with a warning logged
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "fix test" | Re-trigger Greptile |
| from onyx.server.features.mcp.api import UNUSED_RETURN_PATH | ||
| from onyx.server.features.mcp.api import make_oauth_provider |
There was a problem hiding this comment.
Deferred imports inside hot path
make_oauth_provider and UNUSED_RETURN_PATH are imported inside run() on every OAUTH tool invocation. Python's module cache means this is safe (no repeated I/O), but deferred inline imports are a code smell that usually signals a circular-import problem. The comment in the code doesn't explain why the circular import exists; it only describes what the parameters are.
If the circular import can't be broken, at minimum a comment should explain the dependency cycle. If it can be broken (e.g., by moving make_oauth_provider to a lower-level module that mcp_tool.py doesn't transitively pull from), that would be preferred — the best-practices guide flags hidden import-time coupling as an issue to avoid.
Prompt To Fix With AI
This is a comment left during a code review.
Path: backend/onyx/tools/tool_implementations/mcp/mcp_tool.py
Line: 220-221
Comment:
**Deferred imports inside hot path**
`make_oauth_provider` and `UNUSED_RETURN_PATH` are imported inside `run()` on every OAUTH tool invocation. Python's module cache means this is safe (no repeated I/O), but deferred inline imports are a code smell that usually signals a circular-import problem. The comment in the code doesn't explain *why* the circular import exists; it only describes what the parameters are.
If the circular import can't be broken, at minimum a comment should explain the dependency cycle. If it *can* be broken (e.g., by moving `make_oauth_provider` to a lower-level module that `mcp_tool.py` doesn't transitively pull from), that would be preferred — the best-practices guide flags hidden import-time coupling as an issue to avoid.
How can I resolve this? If you propose a fix, please make it concise.| # For OAuth servers, construct OAuthClientProvider so the MCP SDK | ||
| # can refresh expired tokens automatically | ||
| auth: OAuthClientProvider | None = None | ||
| if ( | ||
| self.mcp_server.auth_type == MCPAuthenticationType.OAUTH | ||
| and self.connection_config is not None | ||
| and self._user_id | ||
| ): | ||
| if self.mcp_server.transport == MCPTransport.SSE: | ||
| logger.warning( | ||
| f"MCP tool '{self._name}': OAuth token refresh is not supported " | ||
| f"for SSE transport — auth provider will be ignored. " | ||
| f"Re-authentication may be required after token expiry." | ||
| ) | ||
| else: | ||
| from onyx.server.features.mcp.api import UNUSED_RETURN_PATH | ||
| from onyx.server.features.mcp.api import make_oauth_provider | ||
|
|
||
| # user_id is the requesting user's UUID; safe here because | ||
| # UNUSED_RETURN_PATH ensures redirect_handler raises immediately | ||
| # and user_id is never consulted for Redis state lookups. | ||
| auth = make_oauth_provider( | ||
| self.mcp_server, | ||
| self._user_id, | ||
| UNUSED_RETURN_PATH, | ||
| self.connection_config.id, | ||
| None, | ||
| ) |
There was a problem hiding this comment.
Missing test coverage for the new auth provider path
The new OAuth-provider branch (lines 205–232) is the primary new behavior in this PR but has no dedicated test. The existing test class only covers the pass-through OAuth flow. The SSE warning path, token-refresh happy path, and the reconnect-error path are all untested.
Consider adding tests that verify:
- An OAuth-type server with a valid connection config causes the auth provider to be non-None
- The SSE transport path results in auth being skipped with a warning logged
Prompt To Fix With AI
This is a comment left during a code review.
Path: backend/onyx/tools/tool_implementations/mcp/mcp_tool.py
Line: 205-232
Comment:
**Missing test coverage for the new auth provider path**
The new OAuth-provider branch (lines 205–232) is the primary new behavior in this PR but has no dedicated test. The existing test class only covers the pass-through OAuth flow. The SSE warning path, token-refresh happy path, and the reconnect-error path are all untested.
Consider adding tests that verify:
- An OAuth-type server with a valid connection config causes the auth provider to be non-None
- The SSE transport path results in auth being skipped with a warning logged
How can I resolve this? If you propose a fix, please make it concise.
Automated mirror of PR #9267 so private CI can run.