mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-25 20:15:01 +03:00
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).
This commit is contained in:
@@ -5,7 +5,9 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/web/mcp"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/web/middleware"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/web/service"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/web/service/panel"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/web/service/tgbot"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/web/session"
|
||||
@@ -23,17 +25,29 @@ type APIController struct {
|
||||
settingController *SettingController
|
||||
xraySettingController *XraySettingController
|
||||
userService panel.UserService
|
||||
settingService service.SettingService
|
||||
apiTokenService panel.ApiTokenService
|
||||
Tgbot tgbot.Tgbot
|
||||
}
|
||||
|
||||
// NewAPIController creates a new APIController instance and initializes its routes.
|
||||
func NewAPIController(g *gin.RouterGroup) *APIController {
|
||||
// router is the full engine; MCP tool calls re-enter it as ordinary API requests.
|
||||
func NewAPIController(g *gin.RouterGroup, router http.Handler) *APIController {
|
||||
a := &APIController{}
|
||||
a.initRouter(g)
|
||||
a.initRouter(g, router)
|
||||
return a
|
||||
}
|
||||
|
||||
// requireMCPEnabled hides the MCP endpoint until the operator turns it on: it hands
|
||||
// a token's whole API reach to whatever agent holds that token.
|
||||
func (a *APIController) requireMCPEnabled(c *gin.Context) {
|
||||
if enabled, err := a.settingService.GetMcpEnable(); err != nil || !enabled {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
|
||||
func (a *APIController) checkAPIAuth(c *gin.Context) {
|
||||
setAuthenticated := func(scope string) {
|
||||
if u, err := a.userService.GetFirstUser(); err == nil {
|
||||
@@ -193,7 +207,7 @@ func relAPIPath(fullPath string) string {
|
||||
}
|
||||
|
||||
// initRouter sets up the API routes for inbounds, server, and other endpoints.
|
||||
func (a *APIController) initRouter(g *gin.RouterGroup) {
|
||||
func (a *APIController) initRouter(g *gin.RouterGroup, router http.Handler) {
|
||||
// Main API group
|
||||
api := g.Group("/panel/api")
|
||||
api.Use(a.checkAPIAuth)
|
||||
@@ -236,6 +250,7 @@ func (a *APIController) initRouter(g *gin.RouterGroup) {
|
||||
|
||||
// Extra routes
|
||||
api.POST("/backuptotgbot", a.BackuptoTgbot)
|
||||
api.POST(mcp.Path, a.requireMCPEnabled, gin.WrapH(mcp.NewHandler(router)))
|
||||
}
|
||||
|
||||
// BackuptoTgbot sends a backup of the panel data to Telegram bot admins.
|
||||
|
||||
Reference in New Issue
Block a user