跳到主内容

MCP授权和范围

The Casdoor MCP server authorizes tool calls by OAuth 2.0 scopes. A request that is authenticated with an access token can call only the tools that the scopes of the token allow. This lets you issue tokens with the least privilege that a task needs.

A request that is authenticated with a session cookie isn't checked against scopes and can call all tools.

How scopes control tools​

Each tool requires one scope. A scope has the form resource:action: the resource is the kind of object, and the action is read or write. For example:

  • application:read allows get_applications and get_application.
  • application:write allows add_application, update_application, and delete_application.

tools/list with a token returns only the tools that the token can call. A request without credentials still receives the full list for discovery, but can't call any tool.

Some tools have requirements beyond the scope:

  • Creating an application counts against the application quota of the organization.
  • Applications with an IP allowlist are subject to that check.
  • In demo mode, Casdoor rejects write operations.

Scope reference​

Application scopes​

作用域映射的工具描述
application:readget_applications、get_application查看应用程序配置和设置
application:writeadd_application、update_application、delete_application创建、修改和删除应用程序

User scopes​

作用域映射的工具描述
user:readget_users、get_user查看用户个人资料和信息
user:writeadd_user、update_user、delete_user创建、修改和删除用户账户

Organization scopes​

作用域映射的工具描述
organization:readget_organizations、get_organization查看组织详情和设置
organization:writeadd_organization、update_organization、delete_organization创建、修改和删除组织

Role scopes​

作用域映射的工具映射的工具
role:readget_roles、get_role查看角色定义和分配
role:writeadd_role, update_role, delete_role创建、修改和删除角色

Permission scopes​

作用域映射的工具描述
permission:readget_permissions、get_permission查看权限配置
permission:writeadd_permission、update_permission、delete_permission创建、修改和删除权限

Provider scopes​

作用域映射的工具描述
provider:readget_providers、get_provider查看OAuth、短信、电子邮件及其他提供商配置
provider:writeadd_provider、update_provider、delete_provider创建、修改和删除提供商集成

Token scopes​

作用域映射的工具描述
token:readget_tokens、get_token查看访问令牌及其元数据
token:writedelete_token删除访问令牌

Alias scopes​

Three aliases stand for groups of scopes:

AliasStands for
readEvery :read scope
writeEvery :write scope
adminEvery :read and :write scope

Request a token with scopes​

Name the scopes in the token request. For a token that can read applications but can't change them:

curl -X POST https://your-casdoor.com/api/login/oauth/access_token \
-d "grant_type=client_credentials" \
-d "client_id=YOUR_CLIENT_ID" \
-d "client_secret=YOUR_CLIENT_SECRET" \
-d "scope=application:read"

For a token that can also create and change applications:

curl -X POST https://your-casdoor.com/api/login/oauth/access_token \
-d "grant_type=client_credentials" \
-d "client_id=YOUR_CLIENT_ID" \
-d "client_secret=YOUR_CLIENT_SECRET" \
-d "scope=application:write"

Separate several scopes with spaces: scope=application:read application:write. In a URL, encode the space as %20.

Missing scopes​

When a token calls a tool without the required scope, Casdoor returns the error insufficient_scope:

{
"jsonrpc": "2.0",
"id": 10,
"error": {
"code": -32001,
"message": "insufficient_scope",
"data": {
"tool": "add_application",
"granted_scopes": ["application:read"],
"required_scope": "application:write"
}
}
}

required_scope is the scope that the tool needs, and granted_scopes are the scopes of your token. Request a new token that includes the required scope.

Define scopes for your own MCP server​

When Casdoor is the OAuth 2.0 provider of an MCP server that you build, define scopes that match the capabilities of your server.

  1. In the Casdoor admin console, open the edit page of the application and set Category to Agent.
  2. Add your scopes to the application, each with a name, a display name, and a description. See Custom scopes.
  3. In your MCP server, read the granted scopes from the scope claim of the access token and check them before you run a tool.

The scopes appear in the discovery document of the application and on the consent screen.

For example, a file management server could define:

名称显示名称描述
files:read读取文件查看并下载存储中的文件
files:write写文件创建、修改和删除文件
metadata:read读取元数据查看文件元数据和属性

When a user authorizes an MCP client, Casdoor asks for consent if both of the following conditions hold:

  • The application defines custom scopes.
  • The client requests at least one of them, and the user hasn't granted it to the application before.

The consent screen lists the requested scopes with their display names and descriptions. After the user clicks Allow, Casdoor remembers the grant and doesn't ask again for the same scopes. Users and administrators can see and revoke grants on the Consents page.

Casdoor doesn't show a consent screen for applications without custom scopes.

Add fine-grained rules with Casbin​

Scopes answer the question of which tools a client may call. For rules that depend on the user or the object, use the permissions of Casdoor, which are built on Casbin. A Casbin policy can decide by:

  • Attributes of the user, such as the organization, the role, or the department
  • Properties of the object, such as its owner
  • The environment, such as the time or the IP address
  • Relationships, such as "the user owns the object"

要在您的MCP服务器中使用Casbin:

  1. Define a Casbin model in Casdoor that describes your rules.
  2. Create a permission that connects your application to the model.
  3. Add policies that map users and roles to actions.
  4. In your MCP server, check the permission after you have checked the scope.

A request must pass both checks.

See also​