Files
naiba 083bc985c5 feat(auth): split inventory scope out of server scope
Carve a new nezha:inventory:{read,delete,*} resource family out of
nezha:server:*. Listing and deleting servers/server-groups (GET /server,
/server-group, /ws/server, batch-delete/server[-group], MCP server.list)
now require nezha:inventory:*, while nezha:server:* covers per-server
runtime operations (get, exec, fs, config, metrics, batch-move,
force-update).

Also declare MCP tool OutputSchema for exec/fs/meta/server/transfer and
wrap server.list output in {servers,count} so strict MCP clients accept
the structured result. Correct the /file all-of scope entry and the stale
server:read documentation.
2026-05-31 15:14:41 +00:00

62 lines
1.8 KiB
Go

package controller
import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/nezhahq/nezha/model"
)
// meta.whoami 让 LLM 启动时知道自己拿的是哪张 PAT、能干什么、能动哪些服务器。
// 不要求任何 scope(任意有效 PAT 均可调用)。
type whoamiResult struct {
UserID uint64 `json:"user_id"`
IsAdmin bool `json:"is_admin"`
TokenID uint64 `json:"token_id"`
TokenName string `json:"token_name"`
Scopes []string `json:"scopes"`
ServerIDs []uint64 `json:"server_ids,omitempty"`
}
func init() {
registerMCPTool(&mcpTool{
Name: "meta.whoami",
Description: "Return the identity, scopes and accessible server IDs of the current API token.",
InputSchema: map[string]any{
"type": "object",
"properties": map[string]any{},
},
OutputSchema: map[string]any{
"type": "object",
"properties": map[string]any{
"user_id": map[string]any{"type": "integer"},
"is_admin": map[string]any{"type": "boolean"},
"token_id": map[string]any{"type": "integer"},
"token_name": map[string]any{"type": "string"},
"scopes": map[string]any{"type": "array", "items": map[string]any{"type": "string"}},
"server_ids": map[string]any{"type": "array", "items": map[string]any{"type": "integer"}},
},
"required": []string{"user_id", "is_admin", "token_id", "scopes"},
},
RequiredScope: "",
Handler: handleMetaWhoami,
})
}
func handleMetaWhoami(c *gin.Context, _ json.RawMessage) (any, error) {
tok := APITokenFromContext(c)
if tok == nil {
return nil, errNoToken
}
user, _ := c.MustGet(model.CtxKeyAuthorizedUser).(*model.User)
return whoamiResult{
UserID: user.ID,
IsAdmin: user.Role.IsAdmin(),
TokenID: tok.ID,
TokenName: tok.Name,
Scopes: tok.Scopes(),
ServerIDs: tok.ServerIDs(),
}, nil
}