[GH-ISSUE #6932] Proposal: built-in MCP endpoint so AI agents can manage the panel #4585

Open
opened 2026-10-31 20:40:42 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @sepehr071 on GitHub (Oct 25, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6932

Proposal: built-in MCP endpoint so AI agents can manage the panel

What
Add a Model Context Protocol server to the panel at POST /panel/api/mcp, so agents such as Claude, Cursor or VS Code Copilot can manage inbounds, clients, traffic, nodes, settings and Xray through the API token operators already use.

How (proposed design)

  • New package internal/web/mcp, using the official Go SDK (github.com/modelcontextprotocol/go-sdk), stateless streamable-HTTP transport.
  • The route sits in the existing /panel/api group, so checkAPIAuth, enforceTokenScope and CSRF apply unchanged. Only admin tokens can reach it; monitor / node-sync scopes are denied.
  • Every tool dispatches in-process to the existing REST handlers with the caller's Authorization header. No business logic is duplicated, and all state changes still go through runtime.Runtime, so multi-node setups stay consistent.
  • A curated set of workflow tools (list/get/add/update/delete inbounds and clients, traffic and online status, share/sub links, server status and logs, Xray restart, nodes, hosts, settings), annotated read-only / destructive so clients can ask before destructive calls. A generic call_api tool covers the rest of /panel/api/*.
  • Full endpoint chain: endpoints.ts, make gen, OpenAPI + docs regeneration, tests per CLAUDE.md.

Client config example

{ "mcpServers": { "3x-ui": { "type": "http", "url": "https://panel.example.com/<basepath>/panel/api/mcp",
  "headers": { "Authorization": "Bearer <admin API token>" } } } }

Questions for maintainers

  1. Is a built-in endpoint welcome, or would you prefer a separate project?
  2. Should it be on by default (it is admin-token only), or behind a setting toggle?

I'm happy to send the PR.

Originally created by @sepehr071 on GitHub (Oct 25, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6932 ### Proposal: built-in MCP endpoint so AI agents can manage the panel **What** Add a [Model Context Protocol](https://modelcontextprotocol.io) server to the panel at `POST /panel/api/mcp`, so agents such as Claude, Cursor or VS Code Copilot can manage inbounds, clients, traffic, nodes, settings and Xray through the API token operators already use. **How (proposed design)** - New package `internal/web/mcp`, using the official Go SDK (`github.com/modelcontextprotocol/go-sdk`), stateless streamable-HTTP transport. - The route sits in the existing `/panel/api` group, so `checkAPIAuth`, `enforceTokenScope` and CSRF apply unchanged. Only admin tokens can reach it; monitor / node-sync scopes are denied. - Every tool dispatches in-process to the existing REST handlers with the caller's `Authorization` header. No business logic is duplicated, and all state changes still go through `runtime.Runtime`, so multi-node setups stay consistent. - A curated set of workflow tools (list/get/add/update/delete inbounds and clients, traffic and online status, share/sub links, server status and logs, Xray restart, nodes, hosts, settings), annotated read-only / destructive so clients can ask before destructive calls. A generic `call_api` tool covers the rest of `/panel/api/*`. - Full endpoint chain: `endpoints.ts`, `make gen`, OpenAPI + docs regeneration, tests per `CLAUDE.md`. **Client config example** ```json { "mcpServers": { "3x-ui": { "type": "http", "url": "https://panel.example.com/<basepath>/panel/api/mcp", "headers": { "Authorization": "Bearer <admin API token>" } } } } ``` **Questions for maintainers** 1. Is a built-in endpoint welcome, or would you prefer a separate project? 2. Should it be on by default (it is admin-token only), or behind a setting toggle? I'm happy to send the PR.
Author
Owner

@github-actions[bot] commented on GitHub (Oct 25, 2026):

This is a valid feature request, and the design you describe fits how the panel's API works today. Whether it ships built-in, and whether it is on by default, is the maintainer's decision. I can't answer your two questions for them. Note that #6658 asks for the same feature, and a collaborator (NikanZeyaei) has already offered there to build it. Please coordinate on that thread before you start a PR so the two efforts don't collide.

Checked against the default branch:

  • There is no MCP code yet. A repository search for mcp / modelcontextprotocol finds nothing outside docs/pnpm-lock.yaml, and go.mod has no MCP SDK.
  • Restricted tokens are already denied, as you expect. enforceTokenScope (internal/web/controller/api.go:142) lets admin through with no check. For monitor, node-sync and node-admin it allows only routes in fixed allowlists (monitorScopeAllow at :84, nodeSyncScopeAllow at :99, nodeAdminScopeAllow at :127) and otherwise returns "this API token is not permitted to access this endpoint". A new /mcp route in no allowlist would be admin-only for tokens.
  • Logged-in browser sessions can reach it too, not only admin tokens. checkAPIAuth falls through to session.IsLogin(c) (api.go:67). A session request sets no api_token_scope, so enforceTokenScope passes it (api.go:143-147). Your plan to forward "the caller's Authorization header" therefore needs a rule for cookie-session callers, who send no such header. The simplest rule is to reject requests without a bearer token.
  • CSRF does not block bearer clients. CSRFMiddleware returns early when api_authed is set (internal/web/middleware/security.go:42), so an MCP client sending Authorization: Bearer … needs no CSRF token.
  • Forwarding into the existing handlers covers multi-node. CLAUDE.md requires every state-changing inbound/client operation to go through runtime.Runtime. Calling the existing REST handlers keeps that true without copying business logic.
  • The usual chain for a new route applies. It needs an endpoints.ts entry pinned by TestRouteRegistryContract, then make gen, then copying frontend/public/openapi.json to docs/public/openapi.json and running pnpm gen:api. Any new response struct also needs an entry in the openapigen StructAllow list. Your proposal already lists most of these.

Usable today without code changes: the panel serves its OpenAPI spec at /panel/api/openapi.json (api.go:203). A generic OpenAPI-to-MCP bridge running on your own machine can use it with an admin token, or with a monitor token for read-only access.

Verdict: Feature request

This reply was generated automatically; a maintainer may follow up.

<!-- gh-comment-id:6394725796 --> @github-actions[bot] commented on GitHub (Oct 25, 2026): This is a valid feature request, and the design you describe fits how the panel's API works today. Whether it ships built-in, and whether it is on by default, is the maintainer's decision. I can't answer your two questions for them. Note that **#6658 asks for the same feature**, and a collaborator (`NikanZeyaei`) has already offered there to build it. Please coordinate on that thread before you start a PR so the two efforts don't collide. **Checked against the default branch:** - **There is no MCP code yet.** A repository search for `mcp` / `modelcontextprotocol` finds nothing outside `docs/pnpm-lock.yaml`, and `go.mod` has no MCP SDK. - **Restricted tokens are already denied, as you expect.** `enforceTokenScope` (`internal/web/controller/api.go:142`) lets `admin` through with no check. For `monitor`, `node-sync` and `node-admin` it allows only routes in fixed allowlists (`monitorScopeAllow` at `:84`, `nodeSyncScopeAllow` at `:99`, `nodeAdminScopeAllow` at `:127`) and otherwise returns `"this API token is not permitted to access this endpoint"`. A new `/mcp` route in no allowlist would be admin-only for tokens. - **Logged-in browser sessions can reach it too, not only admin tokens.** `checkAPIAuth` falls through to `session.IsLogin(c)` (`api.go:67`). A session request sets no `api_token_scope`, so `enforceTokenScope` passes it (`api.go:143-147`). Your plan to forward "the caller's `Authorization` header" therefore needs a rule for cookie-session callers, who send no such header. The simplest rule is to reject requests without a bearer token. - **CSRF does not block bearer clients.** `CSRFMiddleware` returns early when `api_authed` is set (`internal/web/middleware/security.go:42`), so an MCP client sending `Authorization: Bearer …` needs no CSRF token. - **Forwarding into the existing handlers covers multi-node.** `CLAUDE.md` requires every state-changing inbound/client operation to go through `runtime.Runtime`. Calling the existing REST handlers keeps that true without copying business logic. - **The usual chain for a new route applies.** It needs an `endpoints.ts` entry pinned by `TestRouteRegistryContract`, then `make gen`, then copying `frontend/public/openapi.json` to `docs/public/openapi.json` and running `pnpm gen:api`. Any new response struct also needs an entry in the openapigen `StructAllow` list. Your proposal already lists most of these. **Usable today without code changes:** the panel serves its OpenAPI spec at `/panel/api/openapi.json` (`api.go:203`). A generic OpenAPI-to-MCP bridge running on your own machine can use it with an `admin` token, or with a `monitor` token for read-only access. Verdict: Feature request *This reply was generated automatically; a maintainer may follow up.* <!-- claude-issue:analyst -->
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#4585
No description provided.