fix(ENG-14445): Fixed the package analytics endpoint generation by mcp, included v2 in it - #417
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes MCP tool URL construction so dynamically generated tools for Cloudsmith API v2 include the required /v2 prefix (derived from the OpenAPI spec’s servers[].url for v2 or basePath for v1), preventing authenticated v2 calls from incorrectly hitting unversioned endpoints and returning 404s.
Changes:
- Added
DynamicMCPServer._spec_base_url()to compute a version-aware base URL from the loaded spec while preserving the configured API host. - Updated tool generation to accept and use a per-spec
base_urlinstead of always usingself.api_base_url. - Added unit tests validating base URL derivation and that a v2 tool is generated under the
/v2/...path.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| cloudsmith_cli/core/mcp/server.py | Builds a version-aware base URL from spec metadata and uses it when generating MCP tools so v2 endpoints resolve correctly. |
| cloudsmith_cli/cli/tests/commands/test_mcp.py | Adds coverage to ensure v2 uses servers[].url path prefix, v1 falls back to basePath, and generated tool URLs include /v2. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
Nice fix. This one was a real gap. I ran a quick live check against a real repo to confirm the premise before digging into the code: The root cause and the fix's target URL both check out. A few things worth addressing or at least considering:
I hope this is helpful as an initial pre-review. If you feed this output into AI, ask the agent to "dispatch an adversarial sub-agent" to challenge this review. You might be impressed by the outcome. |
Use the version from the spec discovery loop as the tool base URL. The spec is fetched from the same prefix, so the host serves it. Remove the _spec_base_url helper and the optional base_url fallback. Replace the five new tests with one test of load_openapi_spec. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7eb3249 to
0dfefad
Compare
|
I rebased this branch on master and pushed a smaller fix in 0dfefad.
v1 tools now use I replaced the five new tests with one test that goes through |
Description
The Cloudsmith MCP server was returning 404 for every v2 backed tool, even when authentication was valid and the workspace slug was correct. This first surfaced with a customer using analytics_logs_package_list, but it affected all v2 endpoints.
The root cause is in how the server builds request URLs. It generates tools dynamically from Cloudsmith's OpenAPI specs and builds each request URL by joining the API host with the path taken from the spec. The version prefix (/v1 or /v2) is not part of the path keys though. It is declared separately: the v2 spec puts it in the servers section (https://api.cloudsmith.io/v2/) and the v1 spec puts it in basePath. The server was ignoring that, so a v2 tool was sent to https://api.cloudsmith.io/analytics/logs/package/{workspace}/ instead of https://api.cloudsmith.io/v2/analytics/logs/package/{workspace}/, which 404s.
v1 tools were never affected because the v1 API also answers at the un-versioned root, so dropping the prefix was harmless for v1. The v2 API only answers under /v2/, so every v2 tool failed. This is a client side URL construction bug, not a permissions or plan issue.
This PR adds a helper that reads the version prefix from the right place for whichever spec is being loaded (the servers URL for v2, or basePath for v1) and prepends it to the configured host. Tool generation now uses that version aware base URL. The fix keeps the configured API host and only borrows the version path from the spec, so a custom API host still works, and v1 behaviour is unchanged.
Type of Change
Bug fix
Additional Notes
Added some tests also.