[PR #6956] feat(mcp): add built-in MCP server at /panel/api/mcp #6540

Open
opened 2026-11-02 04:09:29 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6956
Author: @sepehr071
Created: 10/26/2026
Status: 🔄 Open

Base: main ← Head: feat/mcp-server


📝 Commits (2)

  • 9f5bd6c feat(mcp): add built-in MCP server at /panel/api/mcp
  • 2d080b1 fix(mcp): fence untrusted log text and surface redirects

📊 Changes

22 files changed (+2576 additions, -4 deletions)

View changed files

📝 CLAUDE.md (+6 -0)
➕ docs/content/docs/en/reference/api/mcp.mdx (+37 -0)
📝 docs/content/docs/en/reference/api/meta.json (+1 -0)
📝 docs/public/openapi.json (+60 -0)
📝 frontend/public/openapi.json (+60 -0)
📝 frontend/src/pages/api-docs/endpoints.ts (+25 -0)
📝 go.mod (+6 -0)
📝 go.sum (+14 -0)
📝 internal/web/controller/api.go (+6 -3)
➕ internal/web/mcp/mcp.go (+218 -0)
➕ internal/web/mcp/mcp_test.go (+404 -0)
➕ internal/web/mcp/tools.go (+233 -0)
➕ internal/web/mcp/tools_clients.go (+349 -0)
➕ internal/web/mcp/tools_clients_test.go (+172 -0)
➕ internal/web/mcp/tools_inbounds.go (+110 -0)
➕ internal/web/mcp/tools_inbounds_test.go (+77 -0)
➕ internal/web/mcp/tools_panel.go (+151 -0)
➕ internal/web/mcp/tools_panel_test.go (+53 -0)
➕ internal/web/mcp/tools_server.go (+269 -0)
➕ internal/web/mcp/tools_server_test.go (+144 -0)

...and 2 more files

📄 Description

Closes #6932. Closes #6658.

What

A built-in Model Context Protocol server at POST /panel/api/mcp. MCP clients (Claude, Cursor, VS Code) can manage inbounds, clients, server/Xray, nodes, hosts and settings with an admin API token.

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

How

  • New package internal/web/mcp, using the official Go SDK (github.com/modelcontextprotocol/go-sdk v1.8.0) in stateless streamable-HTTP mode.
  • The route sits in the existing /panel/api group, so checkAPIAuth, enforceTokenScope and CSRF apply unchanged. Restricted scopes (monitor, node-sync, node-admin) get 403. A logged-in browser session must pass the usual CSRF check.
  • No business logic is duplicated. Each tool re-enters the panel router in-process (dispatcher.call) and forwards the caller's Authorization, Cookie, CSRF and proxy headers. Validation, token scopes and runtime.Runtime dispatch therefore run exactly as they do for REST, and multi-node setups stay consistent.
  • 44 task-level tools in 4 toolsets: inbounds (6), clients (13), server (10) and panel (13), plus 2 core tools. Each tool covers one user task, and arguments select the variant (for example save_client creates without email and updates with it). Tools carry read-only / destructive annotations.
  • ?toolsets=clients,inbounds loads only some groups, for clients with tool limits (Cursor caps near 40). An unknown name returns 400.
  • call_api reaches any other /panel/api/* route. It refuses .. and recursion into /mcp. get_openapi_spec gives agents a route index, or one prefix's operations plus the schemas they reference, instead of the 515 KB spec.
  • API token create/delete, panel self-update and DB import/export have no dedicated tools. They are still reachable through call_api, exactly as an admin token can reach them over REST; nothing escalates.
  • get_logs output is fenced as untrusted data and the tool is marked openWorldHint, because failed-login usernames and client traffic put text written by unauthenticated parties into the logs. Every other tool is marked closed-world.
  • Each tool request is logged (MCP tool request: METHOD path -> status (from ip)), so agent-driven changes can be told apart from REST calls (#6658 audit-trail request).

Endpoint chain

endpoints.ts entry (with the toolsets param), make gen, docs/public/openapi.json copied, pnpm gen:api (new reference/api/mcp.mdx, added to meta.json). CLAUDE.md repo map updated.

Tests

  • TestToolsHitDocumentedRoutes: drives every tool through a real MCP client. It asserts the exact method, path, query and body each tool sends, and checks that the route exists in endpoints.ts (which TestRouteRegistryContract pins to the real router). A tool without a case fails the test.
  • TestMCPEndpointUsesPanelAuth (real router, real DB): anonymous gets 404, a monitor token gets 403, and an admin token's call_api reaches the real handler. Verified red when Authorization forwarding is removed.
  • Per-toolset validation tests (bad selectors send no request), TestToolsetsFilterTools, TestCallAPIRejectsPathsOutsideAPI, TestToolResultMapsPanelReplies, TestDescribeOpenAPI.
  • Manual: a live panel in Docker driven over MCP: create inbound, create client, share link, search, disable, delete.

make verify passes locally (Docker, Linux): gen-check, lint, format, typecheck, Go and frontend tests, build, Storybook build, docs OpenAPI check. Not run locally: Postgres, node-e2e and race jobs.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6956 **Author:** [@sepehr071](https://github.com/sepehr071) **Created:** 10/26/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/mcp-server` --- ### 📝 Commits (2) - [`9f5bd6c`](https://github.com/MHSanaei/3x-ui/commit/9f5bd6cbb458539ea84b2d75b5b6c0202a1678a5) feat(mcp): add built-in MCP server at /panel/api/mcp - [`2d080b1`](https://github.com/MHSanaei/3x-ui/commit/2d080b16f4372b841ef9028b1cd98121714628cc) fix(mcp): fence untrusted log text and surface redirects ### 📊 Changes **22 files changed** (+2576 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `CLAUDE.md` (+6 -0) ➕ `docs/content/docs/en/reference/api/mcp.mdx` (+37 -0) 📝 `docs/content/docs/en/reference/api/meta.json` (+1 -0) 📝 `docs/public/openapi.json` (+60 -0) 📝 `frontend/public/openapi.json` (+60 -0) 📝 `frontend/src/pages/api-docs/endpoints.ts` (+25 -0) 📝 `go.mod` (+6 -0) 📝 `go.sum` (+14 -0) 📝 `internal/web/controller/api.go` (+6 -3) ➕ `internal/web/mcp/mcp.go` (+218 -0) ➕ `internal/web/mcp/mcp_test.go` (+404 -0) ➕ `internal/web/mcp/tools.go` (+233 -0) ➕ `internal/web/mcp/tools_clients.go` (+349 -0) ➕ `internal/web/mcp/tools_clients_test.go` (+172 -0) ➕ `internal/web/mcp/tools_inbounds.go` (+110 -0) ➕ `internal/web/mcp/tools_inbounds_test.go` (+77 -0) ➕ `internal/web/mcp/tools_panel.go` (+151 -0) ➕ `internal/web/mcp/tools_panel_test.go` (+53 -0) ➕ `internal/web/mcp/tools_server.go` (+269 -0) ➕ `internal/web/mcp/tools_server_test.go` (+144 -0) _...and 2 more files_ </details> ### 📄 Description Closes #6932. Closes #6658. ## What A built-in [Model Context Protocol](https://modelcontextprotocol.io) server at `POST /panel/api/mcp`. MCP clients (Claude, Cursor, VS Code) can manage inbounds, clients, server/Xray, nodes, hosts and settings with an admin API token. ```json { "mcpServers": { "3x-ui": { "type": "http", "url": "https://panel.example.com/<basepath>/panel/api/mcp", "headers": { "Authorization": "Bearer <admin API token>" } } } } ``` ## How - New package `internal/web/mcp`, using the official Go SDK (`github.com/modelcontextprotocol/go-sdk` v1.8.0) in stateless streamable-HTTP mode. - The route sits in the existing `/panel/api` group, so `checkAPIAuth`, `enforceTokenScope` and CSRF apply unchanged. Restricted scopes (monitor, node-sync, node-admin) get 403. A logged-in browser session must pass the usual CSRF check. - **No business logic is duplicated.** Each tool re-enters the panel router in-process (`dispatcher.call`) and forwards the caller's `Authorization`, `Cookie`, CSRF and proxy headers. Validation, token scopes and `runtime.Runtime` dispatch therefore run exactly as they do for REST, and multi-node setups stay consistent. - **44 task-level tools** in 4 toolsets: `inbounds` (6), `clients` (13), `server` (10) and `panel` (13), plus 2 core tools. Each tool covers one user task, and arguments select the variant (for example `save_client` creates without `email` and updates with it). Tools carry read-only / destructive annotations. - `?toolsets=clients,inbounds` loads only some groups, for clients with tool limits (Cursor caps near 40). An unknown name returns 400. - `call_api` reaches any other `/panel/api/*` route. It refuses `..` and recursion into `/mcp`. `get_openapi_spec` gives agents a route index, or one prefix's operations plus the schemas they reference, instead of the 515 KB spec. - API token create/delete, panel self-update and DB import/export have no dedicated tools. They are still reachable through `call_api`, exactly as an admin token can reach them over REST; nothing escalates. - `get_logs` output is fenced as untrusted data and the tool is marked `openWorldHint`, because failed-login usernames and client traffic put text written by unauthenticated parties into the logs. Every other tool is marked closed-world. - Each tool request is logged (`MCP tool request: METHOD path -> status (from ip)`), so agent-driven changes can be told apart from REST calls (#6658 audit-trail request). ## Endpoint chain `endpoints.ts` entry (with the `toolsets` param), `make gen`, `docs/public/openapi.json` copied, `pnpm gen:api` (new `reference/api/mcp.mdx`, added to `meta.json`). `CLAUDE.md` repo map updated. ## Tests - `TestToolsHitDocumentedRoutes`: drives every tool through a real MCP client. It asserts the exact method, path, query and body each tool sends, and checks that the route exists in `endpoints.ts` (which `TestRouteRegistryContract` pins to the real router). A tool without a case fails the test. - `TestMCPEndpointUsesPanelAuth` (real router, real DB): anonymous gets 404, a monitor token gets 403, and an admin token's `call_api` reaches the real handler. Verified red when Authorization forwarding is removed. - Per-toolset validation tests (bad selectors send no request), `TestToolsetsFilterTools`, `TestCallAPIRejectsPathsOutsideAPI`, `TestToolResultMapsPanelReplies`, `TestDescribeOpenAPI`. - Manual: a live panel in Docker driven over MCP: create inbound, create client, share link, search, disable, delete. `make verify` passes locally (Docker, Linux): gen-check, lint, format, typecheck, Go and frontend tests, build, Storybook build, docs OpenAPI check. Not run locally: Postgres, node-e2e and race jobs. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
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#6540
No description provided.