From ac31455d9976c8dba2010ad3a2fcc0c28b88fb43 Mon Sep 17 00:00:00 2001 From: Sepehr Radmard Date: Wed, 25 Nov 2026 00:32:22 +0330 Subject: [PATCH] feat(mcp): add a built-in MCP server at /panel/api/mcp (#6956) AI agents (Claude, Cursor, VS Code) can manage the panel through a stateless streamable-HTTP MCP endpoint. Every tool re-enters the router with the caller's credentials, so authentication, token scopes, CSRF and runtime.Runtime dispatch apply exactly as for the REST API; a restricted token scope is refused, log text is fenced as untrusted data, and each tool call is written to the panel log. The endpoint hands an API token's whole reach to the agent that holds it, so it is off by default: a panel answers 404 until the MCP server is enabled under Settings -> Security -> API Token (the mcpEnable setting). --- CLAUDE.md | 6 + docs/content/docs/en/reference/api/mcp.mdx | 39 ++ docs/content/docs/en/reference/api/meta.json | 1 + docs/public/openapi.json | 68 ++++ frontend/public/openapi.json | 68 ++++ frontend/src/generated/examples.ts | 2 + frontend/src/generated/schemas.ts | 8 + frontend/src/generated/types.ts | 2 + frontend/src/generated/zod.ts | 2 + frontend/src/models/setting.ts | 1 + frontend/src/pages/api-docs/endpoints.ts | 25 ++ frontend/src/pages/settings/SecurityTab.tsx | 10 + frontend/src/schemas/setting.ts | 1 + go.mod | 6 + go.sum | 14 + internal/web/controller/api.go | 21 +- internal/web/entity/entity.go | 1 + internal/web/mcp/mcp.go | 218 ++++++++++ internal/web/mcp/mcp_test.go | 404 +++++++++++++++++++ internal/web/mcp/tools.go | 233 +++++++++++ internal/web/mcp/tools_clients.go | 349 ++++++++++++++++ internal/web/mcp/tools_clients_test.go | 172 ++++++++ internal/web/mcp/tools_inbounds.go | 110 +++++ internal/web/mcp/tools_inbounds_test.go | 77 ++++ internal/web/mcp/tools_panel.go | 151 +++++++ internal/web/mcp/tools_panel_test.go | 53 +++ internal/web/mcp/tools_server.go | 269 ++++++++++++ internal/web/mcp/tools_server_test.go | 144 +++++++ internal/web/mcp_integration_test.go | 216 ++++++++++ internal/web/service/setting.go | 5 + internal/web/translation/ar-EG.json | 2 + internal/web/translation/en-US.json | 2 + internal/web/translation/es-ES.json | 2 + internal/web/translation/fa-IR.json | 2 + internal/web/translation/id-ID.json | 2 + internal/web/translation/ja-JP.json | 2 + internal/web/translation/pt-BR.json | 2 + internal/web/translation/ru-RU.json | 2 + internal/web/translation/sk-SK.json | 2 + internal/web/translation/tr-TR.json | 2 + internal/web/translation/uk-UA.json | 2 + internal/web/translation/vi-VN.json | 2 + internal/web/translation/zh-CN.json | 2 + internal/web/translation/zh-TW.json | 2 + internal/web/web.go | 2 +- 45 files changed, 2702 insertions(+), 4 deletions(-) create mode 100644 docs/content/docs/en/reference/api/mcp.mdx create mode 100644 internal/web/mcp/mcp.go create mode 100644 internal/web/mcp/mcp_test.go create mode 100644 internal/web/mcp/tools.go create mode 100644 internal/web/mcp/tools_clients.go create mode 100644 internal/web/mcp/tools_clients_test.go create mode 100644 internal/web/mcp/tools_inbounds.go create mode 100644 internal/web/mcp/tools_inbounds_test.go create mode 100644 internal/web/mcp/tools_panel.go create mode 100644 internal/web/mcp/tools_panel_test.go create mode 100644 internal/web/mcp/tools_server.go create mode 100644 internal/web/mcp/tools_server_test.go create mode 100644 internal/web/mcp_integration_test.go diff --git a/CLAUDE.md b/CLAUDE.md index 5c0986587..ae2e20e8a 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -63,6 +63,12 @@ file locations when it can answer in one hop. CPU/memory watchdogs, …); full table in `docs/architecture.md` §5.4. - `middleware/`, `entity/`, `global/`, `session/` (CSRF), `network/`, `runtime/` (master/sub-node over mTLS), `websocket/`. + - `mcp/` — MCP server at `POST /panel/api/mcp` (official go-sdk, stateless). + Tools never call services: each re-enters the router via `dispatcher.call` + with the caller's credentials, so auth, scopes and `runtime.Runtime` apply. + One file per toolset (`tools_.go`, `?toolsets=` filter); every tool + needs a `toolCase`, or `TestToolsHitDocumentedRoutes` fails. Niche routes + stay reachable through `call_api` instead of getting their own tool. - `locale/` + `translation/` — i18n, 14 embedded locale JSON files. - `frontend/` — React + TS source (see `frontend/CLAUDE.md`). - `tools/openapigen/` — Go generator that emits frontend types + Zod/JSON schemas diff --git a/docs/content/docs/en/reference/api/mcp.mdx b/docs/content/docs/en/reference/api/mcp.mdx new file mode 100644 index 000000000..4334f4c7b --- /dev/null +++ b/docs/content/docs/en/reference/api/mcp.mdx @@ -0,0 +1,39 @@ +--- +title: MCP +description: Model Context Protocol endpoint for AI agents (Claude, Cursor, VS + Code). Point an MCP client at this URL with an admin Bearer token; restricted + token scopes are refused. +full: true +_openapi: + preload: + - ./public/openapi.json + toc: + - depth: 2 + title: 'Stateless streamable-HTTP MCP endpoint (JSON-RPC 2.0). Exposes panel + tools for inbounds, clients, server, Xray, nodes, hosts and settings; + each tool call runs the matching /panel/api route with the caller’s + credentials. Off by default: answers 404 until the MCP server is enabled + under Settings → Security → API Token.' + url: '#stateless-streamable-http-mcp-endpoint-json-rpc-20-exposes-panel-tools-for-inbounds-clients-server-xray-nodes-hosts-and-settings-each-tool-call-runs-the-matching-panelapi-route-with-the-callers-credentials-off-by-default-answers-404-until-the-mcp-server-is-enabled-under-settings--security--api-token' + structuredData: + headings: + - content: 'Stateless streamable-HTTP MCP endpoint (JSON-RPC 2.0). Exposes panel + tools for inbounds, clients, server, Xray, nodes, hosts and settings; + each tool call runs the matching /panel/api route with the caller’s + credentials. Off by default: answers 404 until the MCP server is + enabled under Settings → Security → API Token.' + id: stateless-streamable-http-mcp-endpoint-json-rpc-20-exposes-panel-tools-for-inbounds-clients-server-xray-nodes-hosts-and-settings-each-tool-call-runs-the-matching-panelapi-route-with-the-callers-credentials-off-by-default-answers-404-until-the-mcp-server-is-enabled-under-settings--security--api-token + contents: [] +--- + +{/* This file was generated by Fumadocs. Do not edit this file directly. Any changes should be made by running the generation command again. */} + +export default function Layout(props) { + const { OpenAPIPage } = props.components ?? {}; + return ( + <> + {props.children} + + + ); +} \ No newline at end of file diff --git a/docs/content/docs/en/reference/api/meta.json b/docs/content/docs/en/reference/api/meta.json index b21d606de..99d41c873 100644 --- a/docs/content/docs/en/reference/api/meta.json +++ b/docs/content/docs/en/reference/api/meta.json @@ -15,6 +15,7 @@ "hosts", "nodes", "backup", + "mcp", "websocket" ] } diff --git a/docs/public/openapi.json b/docs/public/openapi.json index 48febad12..e8f237583 100644 --- a/docs/public/openapi.json +++ b/docs/public/openapi.json @@ -152,6 +152,9 @@ "ldapVlessField": { "type": "string" }, + "mcpEnable": { + "type": "boolean" + }, "networkTrafficInterface": { "example": "eth0", "type": "string" @@ -721,6 +724,7 @@ "ldapUserAttr", "ldapUserFilter", "ldapVlessField", + "mcpEnable", "networkTrafficInterface", "networkTrafficMode", "nodeDownThreshold", @@ -1041,6 +1045,9 @@ "ldapVlessField": { "type": "string" }, + "mcpEnable": { + "type": "boolean" + }, "networkTrafficInterface": { "example": "eth0", "type": "string" @@ -1620,6 +1627,7 @@ "ldapUserAttr", "ldapUserFilter", "ldapVlessField", + "mcpEnable", "networkTrafficInterface", "networkTrafficMode", "nodeDownThreshold", @@ -5562,6 +5570,10 @@ "name": "Backup", "description": "Operations that interact with the configured Telegram bot." }, + { + "name": "MCP", + "description": "Model Context Protocol endpoint for AI agents (Claude, Cursor, VS Code). Point an MCP client at this URL with an admin Bearer token; restricted token scopes are refused." + }, { "name": "Settings", "description": "Panel configuration and user credentials. All endpoints live under /panel/api/setting and require a logged-in session or Bearer token." @@ -14605,6 +14617,62 @@ } } }, + "/panel/api/mcp": { + "post": { + "tags": [ + "MCP" + ], + "summary": "Stateless streamable-HTTP MCP endpoint (JSON-RPC 2.0). Exposes panel tools for inbounds, clients, server, Xray, nodes, hosts and settings; each tool call runs the matching /panel/api route with the caller’s credentials. Off by default: answers 404 until the MCP server is enabled under Settings → Security → API Token.", + "operationId": "post_panel_api_mcp", + "parameters": [ + { + "name": "toolsets", + "in": "query", + "required": false, + "description": "Comma-separated tool groups to expose: inbounds, clients, server, panel. Omit for all. call_api and get_openapi_spec are always included.", + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object" + }, + "example": { + "jsonrpc": "2.0", + "id": 1, + "method": "tools/list" + } + } + } + }, + "responses": { + "200": { + "description": "Successful response", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "success": { + "type": "boolean" + }, + "msg": { + "type": "string" + }, + "obj": {} + } + } + } + } + } + } + } + }, "/panel/api/setting/all": { "post": { "tags": [ diff --git a/frontend/public/openapi.json b/frontend/public/openapi.json index 48febad12..e8f237583 100644 --- a/frontend/public/openapi.json +++ b/frontend/public/openapi.json @@ -152,6 +152,9 @@ "ldapVlessField": { "type": "string" }, + "mcpEnable": { + "type": "boolean" + }, "networkTrafficInterface": { "example": "eth0", "type": "string" @@ -721,6 +724,7 @@ "ldapUserAttr", "ldapUserFilter", "ldapVlessField", + "mcpEnable", "networkTrafficInterface", "networkTrafficMode", "nodeDownThreshold", @@ -1041,6 +1045,9 @@ "ldapVlessField": { "type": "string" }, + "mcpEnable": { + "type": "boolean" + }, "networkTrafficInterface": { "example": "eth0", "type": "string" @@ -1620,6 +1627,7 @@ "ldapUserAttr", "ldapUserFilter", "ldapVlessField", + "mcpEnable", "networkTrafficInterface", "networkTrafficMode", "nodeDownThreshold", @@ -5562,6 +5570,10 @@ "name": "Backup", "description": "Operations that interact with the configured Telegram bot." }, + { + "name": "MCP", + "description": "Model Context Protocol endpoint for AI agents (Claude, Cursor, VS Code). Point an MCP client at this URL with an admin Bearer token; restricted token scopes are refused." + }, { "name": "Settings", "description": "Panel configuration and user credentials. All endpoints live under /panel/api/setting and require a logged-in session or Bearer token." @@ -14605,6 +14617,62 @@ } } }, + "/panel/api/mcp": { + "post": { + "tags": [ + "MCP" + ], + "summary": "Stateless streamable-HTTP MCP endpoint (JSON-RPC 2.0). Exposes panel tools for inbounds, clients, server, Xray, nodes, hosts and settings; each tool call runs the matching /panel/api route with the caller’s credentials. Off by default: answers 404 until the MCP server is enabled under Settings → Security → API Token.", + "operationId": "post_panel_api_mcp", + "parameters": [ + { + "name": "toolsets", + "in": "query", + "required": false, + "description": "Comma-separated tool groups to expose: inbounds, clients, server, panel. Omit for all. call_api and get_openapi_spec are always included.", + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object" + }, + "example": { + "jsonrpc": "2.0", + "id": 1, + "method": "tools/list" + } + } + } + }, + "responses": { + "200": { + "description": "Successful response", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "success": { + "type": "boolean" + }, + "msg": { + "type": "string" + }, + "obj": {} + } + } + } + } + } + } + } + }, "/panel/api/setting/all": { "post": { "tags": [ diff --git a/frontend/src/generated/examples.ts b/frontend/src/generated/examples.ts index 1ae15a8ee..4fbf6aa99 100644 --- a/frontend/src/generated/examples.ts +++ b/frontend/src/generated/examples.ts @@ -39,6 +39,7 @@ export const EXAMPLES: Record = { "ldapUserAttr": "", "ldapUserFilter": "", "ldapVlessField": "", + "mcpEnable": false, "networkTrafficInterface": "eth0", "networkTrafficMode": "legacy", "nodeDownThreshold": 1, @@ -250,6 +251,7 @@ export const EXAMPLES: Record = { "ldapUserAttr": "", "ldapUserFilter": "", "ldapVlessField": "", + "mcpEnable": false, "networkTrafficInterface": "eth0", "networkTrafficMode": "legacy", "nodeDownThreshold": 1, diff --git a/frontend/src/generated/schemas.ts b/frontend/src/generated/schemas.ts index 11ef0e993..54dc16336 100644 --- a/frontend/src/generated/schemas.ts +++ b/frontend/src/generated/schemas.ts @@ -126,6 +126,9 @@ export const SCHEMAS: Record = { "ldapVlessField": { "type": "string" }, + "mcpEnable": { + "type": "boolean" + }, "networkTrafficInterface": { "example": "eth0", "type": "string" @@ -695,6 +698,7 @@ export const SCHEMAS: Record = { "ldapUserAttr", "ldapUserFilter", "ldapVlessField", + "mcpEnable", "networkTrafficInterface", "networkTrafficMode", "nodeDownThreshold", @@ -1015,6 +1019,9 @@ export const SCHEMAS: Record = { "ldapVlessField": { "type": "string" }, + "mcpEnable": { + "type": "boolean" + }, "networkTrafficInterface": { "example": "eth0", "type": "string" @@ -1594,6 +1601,7 @@ export const SCHEMAS: Record = { "ldapUserAttr", "ldapUserFilter", "ldapVlessField", + "mcpEnable", "networkTrafficInterface", "networkTrafficMode", "nodeDownThreshold", diff --git a/frontend/src/generated/types.ts b/frontend/src/generated/types.ts index 269aefccc..01fa5b638 100644 --- a/frontend/src/generated/types.ts +++ b/frontend/src/generated/types.ts @@ -44,6 +44,7 @@ export interface AllSetting { ldapUserAttr: string; ldapUserFilter: string; ldapVlessField: string; + mcpEnable: boolean; networkTrafficInterface: string; networkTrafficMode: string; nodeDownThreshold: number; @@ -256,6 +257,7 @@ export interface AllSettingView { ldapUserAttr: string; ldapUserFilter: string; ldapVlessField: string; + mcpEnable: boolean; networkTrafficInterface: string; networkTrafficMode: string; nodeDownThreshold: number; diff --git a/frontend/src/generated/zod.ts b/frontend/src/generated/zod.ts index f59eb9e08..0e0f933e5 100644 --- a/frontend/src/generated/zod.ts +++ b/frontend/src/generated/zod.ts @@ -54,6 +54,7 @@ export const AllSettingSchema = z.object({ ldapUserAttr: z.string(), ldapUserFilter: z.string(), ldapVlessField: z.string(), + mcpEnable: z.boolean(), networkTrafficInterface: z.string(), networkTrafficMode: z.enum(['legacy', 'auto', 'manual']), nodeDownThreshold: z.number().int().min(1).max(100), @@ -267,6 +268,7 @@ export const AllSettingViewSchema = z.object({ ldapUserAttr: z.string(), ldapUserFilter: z.string(), ldapVlessField: z.string(), + mcpEnable: z.boolean(), networkTrafficInterface: z.string(), networkTrafficMode: z.enum(['legacy', 'auto', 'manual']), nodeDownThreshold: z.number().int().min(1).max(100), diff --git a/frontend/src/models/setting.ts b/frontend/src/models/setting.ts index 2dbaa4611..12f635adf 100644 --- a/frontend/src/models/setting.ts +++ b/frontend/src/models/setting.ts @@ -77,6 +77,7 @@ export class AllSetting { externalTrafficInformEnable = false; externalTrafficInformURI = ''; restartXrayOnClientDisable = true; + mcpEnable = false; subCertFile = ''; subKeyFile = ''; externalSubUserAgent = 'v2rayNG/1.8.5'; diff --git a/frontend/src/pages/api-docs/endpoints.ts b/frontend/src/pages/api-docs/endpoints.ts index fa7d37da2..f8c958038 100644 --- a/frontend/src/pages/api-docs/endpoints.ts +++ b/frontend/src/pages/api-docs/endpoints.ts @@ -2164,6 +2164,31 @@ export const sections: readonly Section[] = [ ], }, + { + id: 'mcp', + title: 'MCP', + description: + 'Model Context Protocol endpoint for AI agents (Claude, Cursor, VS Code). Point an MCP client at this URL with an admin Bearer token; restricted token scopes are refused.', + endpoints: [ + { + method: 'POST', + path: '/panel/api/mcp', + summary: + 'Stateless streamable-HTTP MCP endpoint (JSON-RPC 2.0). Exposes panel tools for inbounds, clients, server, Xray, nodes, hosts and settings; each tool call runs the matching /panel/api route with the caller’s credentials. Off by default: answers 404 until the MCP server is enabled under Settings → Security → API Token.', + params: [ + { + name: 'toolsets', + in: 'query', + type: 'string', + optional: true, + desc: 'Comma-separated tool groups to expose: inbounds, clients, server, panel. Omit for all. call_api and get_openapi_spec are always included.', + }, + ], + body: '{\n "jsonrpc": "2.0",\n "id": 1,\n "method": "tools/list"\n}', + }, + ], + }, + { id: 'settings', title: 'Settings', diff --git a/frontend/src/pages/settings/SecurityTab.tsx b/frontend/src/pages/settings/SecurityTab.tsx index 87e4a5f1a..f82f77a01 100644 --- a/frontend/src/pages/settings/SecurityTab.tsx +++ b/frontend/src/pages/settings/SecurityTab.tsx @@ -375,6 +375,16 @@ export default function SecurityTab({ allSetting, updateSetting, saveSetting }: label: catTabLabel(, t('pages.nodes.apiToken'), isMobile), children: (
+ + updateSetting({ mcpEnable: v })} + /> +

{t('pages.nodes.apiTokenHint')}