Skip to content

Native MCP server at /client/mcp - #14761

Open
ewerton-silva00 wants to merge 5 commits into
apache:mainfrom
ewerton-silva00:feat/mcp-server-upstream
Open

ewerton-silva00 wants to merge 5 commits into
apache:mainfrom
ewerton-silva00:feat/mcp-server-upstream

Conversation

@ewerton-silva00

Copy link
Copy Markdown

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)
  • 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

  • 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.

Add the createMcpToken, listMcpTokens and revokeMcpToken APIs. A token
belongs to the user that creates it, may have an expiration date and is
returned only on creation: the mcp_token table stores its SHA-256 hash.

Add the mcp.enabled and mcp.async.job.wait settings in their own MCP
group of the global settings.
Add a servlet that implements the Streamable HTTP transport of the
Model Context Protocol without streaming and without sessions, for the
protocol revisions 2025-06-18 and 2025-11-25. Requests are authenticated
with an MCP token and run as the user that owns it.

Instead of one tool per API, four tools search, describe and execute
any API available to the user and query asynchronous jobs. APIs go
through ApiServerService, so the role permissions, the allowed source
CIDRs, the API rate limit and the project roles apply as in /client/api.

The server is off by default (mcp.enabled).
Add an MCP Tokens tab to the user view to list and revoke the tokens of
the user and, for the logged in user, to create one. The token is shown
only once, right after it is created.
Accept the 2026-07-28 revision, which drops the initialize handshake: answer server/discover, validate the MCP-Protocol-Version, Mcp-Method and Mcp-Name headers against the request body, and return resultType and cache hints. The 2025-11-25 and 2025-06-18 revisions keep working through initialize.
Users are soft deleted, so the foreign key cascade never fires and the tokens stayed behind. Remove them when a user or its account is deleted, and when a user is moved to another account, since the token keeps the account and domain it was created in.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant