ewerton-silva00 opened a new pull request, #14761:
URL: https://github.com/apache/cloudstack/pull/14761

   ### Description
   
   This PR adds a native MCP (Model Context Protocol) server to the management 
server, served at `/client/mcp`, so MCP clients can use the CloudStack APIs the 
same way CloudMonkey does. There is no extra process and no new dependency: the 
JSON-RPC layer is written on top of the servlet API and Gson, both already in 
the classpath.
   
   Every call goes through the same path as the regular API 
(`ApiServerService.verifyRequest` followed by `handleRequest`), so CIDR 
restrictions, role permissions, rate limiting and `api.key.access` apply 
unchanged. The feature is disabled by default.
   
   **Access tokens**
   
   - New APIs `createMcpToken`, `listMcpTokens` and `revokeMcpToken`, with the 
events `MCP.TOKEN.CREATE` and `MCP.TOKEN.REVOKE`.
   - New table `cloud.mcp_token`. Only the SHA-256 hash is stored; the token 
(`csmcp_…`) is returned once, on creation, and never logged.
   - Optional expiration date. A token inherits the permissions of the role of 
its user and can only be created for the calling user.
   - Tokens are removed when the user or its account is removed, and when the 
user is moved to another account.
   
   **Server**
   
   - Streamable HTTP, stateless, POST only, JSON responses (no SSE, no 
sessions).
   - Protocol revisions `2025-06-18` and `2025-11-25` (with `initialize`) and 
`2026-07-28` (no handshake, `server/discover`, validation of the 
`MCP-Protocol-Version`, `Mcp-Method` and `Mcp-Name` headers).
   - Requests carrying an `Origin` whose host differs from `Host` are rejected.
   - Four tools instead of one per API, so the whole catalogue stays reachable 
without flooding the context of the client:
   
   | Tool | Purpose |
   |---|---|
   | `cloudstack_search_apis` | Search the APIs the caller is allowed to use |
   | `cloudstack_describe_api` | Parameters of one API |
   | `cloudstack_execute_api` | Run an API; async jobs are awaited up to 
`mcp.async.job.wait` |
   | `cloudstack_query_async_job` | Query a pending job |
   
   **Configuration**
   
   New group **MCP** in Global Settings, both settings dynamic:
   
   - `mcp.enabled` (default `false`): when off, `/client/mcp` answers 404.
   - `mcp.async.job.wait` (default `20` seconds).
   
   **UI**
   
   "MCP Tokens" tab in the user view, with a dialog that shows the token once. 
Labels in `en` and `pt_BR`.
   
   **Usage**
   
   ```
   cmk update configuration name=mcp.enabled value=true
   cmk create mcptoken name=my-client
   claude mcp add --transport http cloudstack https://<host>/client/mcp 
--header "Authorization: Bearer <token>"
   ```
   
   **Known limits**
   
   - Clients that require OAuth cannot use a static bearer token.
   - Users of the same account can list and revoke each other's tokens, 
matching the behaviour of API keys.
   - A LIST item containing a comma cannot be represented, as in CloudMonkey.
   
   This change has not been discussed on the dev mailing list yet and has no 
functional specification; I am happy to start that discussion if the feature is 
of interest.
   
   This work was developed with the assistance of an AI coding tool (Claude 
Code).
   
   ### Types of changes
   
   - [ ] Breaking change (fix or feature that would cause existing 
functionality to change)
   - [x] New feature (non-breaking change which adds functionality)
   - [ ] Bug fix (non-breaking change which fixes an issue)
   - [ ] Enhancement (improves an existing feature and functionality)
   - [ ] Cleanup (Code refactoring and cleanup, that may add test cases)
   - [ ] Build/CI
   - [ ] Test (unit or integration test code)
   
   ### Feature/Enhancement Scale or Bug Severity
   
   #### Feature/Enhancement Scale
   
   - [x] Major
   - [ ] Minor
   
   ### How Has This Been Tested?
   
   - Unit tests: new `McpServletTest`, `McpParamEncoderTest` and 
`McpTokenManagerImplTest`, plus the changed `AccountManagerImplTest`. Run 
together with `ApiServletTest` and `ApiServerTest`: 255 tests pass.
   - End to end on a local environment (management server through `mvn 
jetty:run` with the simulator, MySQL, nginx with TLS in front): 61 scripted 
checks pass, covering the toggle through the API, the token lifecycle, the 
three protocol revisions, synchronous and asynchronous calls 
(`deployVirtualMachine`, `destroyVirtualMachine`), LIST and MAP parameters, and 
the removal of tokens with the account and with `moveUser`.
   - Claude Code 2.1.330 connects using revision `2026-07-28` and lists the 
tools.
   
   Not tested:
   
   - The packaged server (`ServerDaemon`), so its 30 s idle timeout and gzip 
handling were not exercised; `mcp.async.job.wait` defaults to 20 s for that 
reason.
   - The token tab and dialog were not covered by the scripted validation.
   - Marvin/integration tests were not written.
   
   #### How did you try to break this feature and the system with this change?
   
   - No `Authorization` header, unknown token, revoked token, expired token and 
a user with `apikeyaccess` disabled all get 401.
   - `Origin` from another host gets 403; GET gets 405; with 
`mcp.enabled=false` the endpoint answers 404.
   - A regular user calling an admin API (`listHosts`) gets a tool error, not 
data.
   - Reserved parameters (`command`, `response`, `apikey`, `signature`, 
`sessionkey`, …) passed as tool arguments are rejected.
   - Invalid JSON, batches, unknown methods, unsupported protocol versions and 
headers that disagree with the body return the corresponding JSON-RPC errors.
   - The management server and nginx logs were checked to confirm the token 
does not appear in them.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to