TL;DR
- Asking for one delegated scope did not narrow the token. The client asked for Mcp.Tools.Read and got every scope the admin had consented, including Mcp.Tools.Write.
- Client credentials accept only /.default. A single app role by name was refused, and /.default returned every role granted to the caller.
- A naive Model Context Protocol (MCP) server that calls Graph with its own app token changed a group for a user who could not. Every tool call ran with the widest permission the server held.
- Narrowing the grant shrank new tokens within 4 to 13 seconds in this small tenant, though answers flapped for about half a minute. A token issued before the change still wrote to the group seven minutes later.
- The fix that held was a per-tool check in the server, on-behalf-of (OBO) for user calls, and narrow grants. Conditional Access for workload identities was not tested.
Repeat this lab
I wrote this lab so you can run it yourself. Each step has the commands to copy and the output I got, and you can tick it as done. Your ticks stay in this browser only.
Copies a ready-made prompt for Claude Code, Cursor, Codex or any coding agent. For test environments only.
Show prompt
I want to run a hands-on lab from iamops.dev, and I would like your help to do it safely. Lab: Delegated or application permissions for an MCP server in Entra, and what /.default really asks for Read the whole lab here (Markdown): https://iamops.dev/entra-mcp-delegated-vs-app-permissions.md What the lab needs before the first step: - Throwaway tenant with Global Administrator - Azure CLI, Python 3.10+ with PyJWT, curl, jq and openssl - Values I have to set myself (the lab uses placeholders): <TENANT_ID> How I want you to work: 1. Read the lab from start to finish first (each step lists its own prerequisites). Then check that the prerequisites are in place and that the tool versions match what the lab expects. Tell me what is missing before you run anything. 2. Run one step at a time. Before each command, show it to me and say in a sentence what it does. 3. Ask me before anything that costs money, creates cloud resources, touches a real tenant or account, or deletes anything. Wait for my answer. 4. Use the placeholders from the lab. Never paste real secrets, tokens or production identities into a command, a file or this chat. If a value is needed, tell me where to set it myself. 5. After each step, compare the output with the expected output in the lab. If it does not match, stop and tell me what differs. Do not carry on or invent a fix without asking me. 6. At the end, report what passed, what failed and which cleanup you did. Then offer to tick the steps off for me. The lab page keeps ticks only in my browser, so if you cannot do that, list the steps I should tick. This lab is for test environments only. Do not run it against production, or against a tenant or account that holds real data or real users. You may answer in my language.
A reader asked why teams building Model Context Protocol (MCP) servers in Entra default to /.default and never narrow it. I wanted to see what Entra actually puts in the token, so I built one MCP server identity, a public MCP client, an autonomous agent, and a deliberately naive tool dispatcher that calls Microsoft Graph with one app token for every tool.
Then I asked Entra for less than the grant, for something never consented, and for /.default, on both the delegated and the application path. The short answer: in Entra the grant decides what a token carries, not the request. Even delegated tokens behaved that way. So the fix is in the grants and in the server, not in the scope string.
The question, answered first. Delegated or application permissions for an MCP server identity? Use delegated scopes with on-behalf-of (OBO) for every tool that acts for a person, so Graph enforces that person’s own rights. Use application roles only for work nobody is signed in for, and keep each role narrow. /.default is not a choice you can skip on the application path: it is the only value Entra accepts there. So “narrow it” means narrowing the grant, which is what the lab does.
Every test below is an az or curl command you run yourself. One file, entra_lab.py, does the two jobs that need code: the setup (three apps, a user, a group and the grants) and the small MCP server for the naive and fixed runs. Several things I did not test, including Conditional Access and an interactive consent page, are listed at the end.
Setup#
You need a throwaway Entra tenant where you are Global Administrator, never a work or production tenant. The setup creates three app registrations, one user, one group, and admin-consent grants, including Group.ReadWrite.All as an application permission. Sign in with the Azure CLI, set TENANT_ID and a secrets folder, and run the setup. Download entra_lab.py into an empty folder first. You need Python 3.10 or newer with PyJWT and its crypto extra, the Azure CLI, curl, jq and openssl:
export TENANT_ID="<TENANT_ID>"
export LAB_SECRETS=~/.lab-mcp-perm
az login --tenant "$TENANT_ID" --allow-no-subscriptions
python3 -m pip install "pyjwt[crypto]"
python3 entra_lab.py setup
source "$LAB_SECRETS/lab.env"The setup keeps the server’s client secret, the test user’s password and the agent’s private key in files in $LAB_SECRETS, readable only by you, and never prints them. lab.env holds only the identifiers the commands below use. Two shell helpers keep those commands short: claims prints the permission claims of a token and nothing else, and b64url is for the agent’s signed request further down.
TOKEN_URL="https://login.microsoftonline.com/$TENANT_ID/oauth2/v2.0/token"
GRAPH=https://graph.microsoft.com/v1.0
claims() { jq -c -R 'split(".")[1] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson | {scp, roles, idtyp}'; }
b64url() { openssl base64 -A | tr '+/' '-_' | tr -d '='; }Before any token, check the tenant’s OIDC discovery document with the iamops.dev OIDC discovery tool. It shows the issuer https://login.microsoftonline.com/<TENANT_ID>/v2.0, the token endpoint, and the jwks_uri the server uses to verify tokens.

setup registers lab-mcp-perm-server with three delegated scopes (Mcp.Tools.Read, Mcp.Tools.Write, Mcp.Admin) and two app roles (Mcp.Tools.Read.All, Mcp.Tools.Write.All). It then grants what an admin would click through: a delegated grant from the client for Read and Write but not Admin, both app roles for the agent, and for the server itself a broad Graph grant with User.Read.All, Group.ReadWrite.All and Application.Read.All as application permissions, plus User.Read and Group.ReadWrite.All delegated.

The test user is a plain member who does not own the group. That matters for the delegated tests.
Both paths#
One run of each path gives the contrast cases something to compare with. The delegated path uses a signed-in user, the client asks Entra for a token, and the server swaps it for a Graph token with on-behalf-of (OBO). The test user signs in with the password grant, which is acceptable only for a throwaway user:
USER_TOKEN=$(curl -s "$TOKEN_URL" -d grant_type=password -d client_id="$CLIENT_APP" \
-d scope="api://$SERVER_APP/Mcp.Tools.Read" --data-urlencode username="$LAB_UPN" \
--data-urlencode "password@$LAB_SECRETS/user.password" | jq -r .access_token)
claims <<< "$USER_TOKEN"
OBO_TOKEN=$(curl -s "$TOKEN_URL" -d grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer \
-d client_id="$SERVER_APP" --data-urlencode "client_secret@$LAB_SECRETS/server.secret" \
-d assertion="$USER_TOKEN" -d requested_token_use=on_behalf_of \
-d scope=https://graph.microsoft.com/.default | jq -r .access_token)
claims <<< "$OBO_TOKEN"The client asked for api://server/Mcp.Tools.Read only. The token came back with scp Mcp.Tools.Read Mcp.Tools.Write, because both were consented. The JWT tool confirms the signature, issuer and audience, and shows scp with both scopes.


The application path has no user. The agent asks for a client credentials token with its certificate. Instead of a secret it sends a short JWT signed with the certificate’s private key, the client assertion. assertion builds a new one, valid for 10 minutes, each time it runs:
X5T=$(openssl x509 -in "$LAB_SECRETS/agent.crt" -outform DER | openssl dgst -sha1 -binary | b64url)
assertion() {
local now hdr pay
now=$(date +%s)
hdr=$(printf '{"alg":"RS256","typ":"JWT","x5t":"%s"}' "$X5T" | b64url)
pay=$(printf '{"aud":"%s","iss":"%s","sub":"%s","jti":"%s","nbf":%d,"exp":%d}' \
"$TOKEN_URL" "$AGENT_APP" "$AGENT_APP" "$(uuidgen)" "$now" "$((now + 600))" | b64url)
printf '%s.%s.%s' "$hdr" "$pay" "$(printf %s "$hdr.$pay" | openssl dgst -sha256 -sign "$LAB_SECRETS/agent.key" | b64url)"
}
AGENT_TOKEN=$(curl -s "$TOKEN_URL" -d grant_type=client_credentials -d client_id="$AGENT_APP" \
-d scope="api://$SERVER_APP/.default" \
-d client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer \
-d client_assertion="$(assertion)" | jq -r .access_token)
claims <<< "$AGENT_TOKEN"The token has roles, not scp, and has_user is false. The agent got Mcp.Tools.Read.All and Mcp.Tools.Write.All, the two roles it was assigned.

Contrast cases#
Ask for a scope nobody consented. The client asked for Mcp.Admin, which is exposed but was never consented. Entra refused it with 400 invalid_grant, AADSTS65001. The server’s OBO request for Graph Mail.Read, which it never had, was refused the same way. Without an interactive consent prompt this is a hard stop. With an interactive client the user would see a consent page instead, and what they may accept depends on the tenant’s user consent policy, which I did not test.
curl -s "$TOKEN_URL" -d grant_type=password -d client_id="$CLIENT_APP" \
-d scope="api://$SERVER_APP/Mcp.Admin" --data-urlencode username="$LAB_UPN" \
--data-urlencode "password@$LAB_SECRETS/user.password" | jq -r '.error, .error_description[0:60]'
curl -s "$TOKEN_URL" -d grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer \
-d client_id="$SERVER_APP" --data-urlencode "client_secret@$LAB_SECRETS/server.secret" \
-d assertion="$USER_TOKEN" -d requested_token_use=on_behalf_of \
-d scope=https://graph.microsoft.com/Mail.Read | jq -r '.error, .error_description[0:60]'Ask for /.default on the delegated path. The client asked for api://server/.default and got the same two scopes as the named request. The server’s OBO request for Graph /.default returned Group.ReadWrite.All User.Read email openid profile. On the delegated path /.default and a named scope ended up the same, because the grant decides.
curl -s "$TOKEN_URL" -d grant_type=password -d client_id="$CLIENT_APP" \
-d scope="api://$SERVER_APP/.default" --data-urlencode username="$LAB_UPN" \
--data-urlencode "password@$LAB_SECRETS/user.password" | jq -r .access_token | claimsAsk client credentials for one app role by name. The agent asked for api://server/Mcp.Tools.Read.All, and the server asked Graph for User.Read.All. Both were refused with 400 invalid_scope, AADSTS1002012: client credentials must use /.default with the resource identifier. That is why app-only servers use /.default. It is the only accepted value, so the only place to narrow is the grant.
curl -s "$TOKEN_URL" -d grant_type=client_credentials -d client_id="$AGENT_APP" \
-d scope="api://$SERVER_APP/Mcp.Tools.Read.All" \
-d client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer \
-d client_assertion="$(assertion)" | jq -r '.error, .error_description[0:60]'
curl -s "$TOKEN_URL" -d grant_type=client_credentials -d client_id="$SERVER_APP" \
--data-urlencode "client_secret@$LAB_SECRETS/server.secret" \
-d scope=https://graph.microsoft.com/User.Read.All | jq -r '.error, .error_description[0:60]'/.default returns every role granted to the caller.The user’s own rights still apply on the delegated path. The OBO Graph token carried Group.ReadWrite.All in scp. The user could read the lab group, but when they tried to change a group they do not own, Graph returned 403 Authorization_RequestDenied. The delegated token is the consented scopes intersected with what the user may do. A broad delegated grant is less dangerous than the same application permission.
curl -s -o /dev/null -w '%{http_code}\n' "$GRAPH/groups/$GROUP_ID?\$select=displayName" \
-H "Authorization: Bearer $OBO_TOKEN"
curl -s -X PATCH "$GRAPH/groups/$GROUP_ID" -H "Authorization: Bearer $OBO_TOKEN" \
-H "Content-Type: application/json" -d '{"description":"changed by the user token"}' | jq -r .error.codeThe server asks Graph for /.default after the broad grant. Now the server asks for its own Graph token, as an app. The same group change that failed for the user went through with 204. The app token listed Application.Read.All, Group.ReadWrite.All and User.Read.All as roles, idtyp app, and no user.
GRAPH_APP_TOKEN=$(curl -s "$TOKEN_URL" -d grant_type=client_credentials -d client_id="$SERVER_APP" \
--data-urlencode "client_secret@$LAB_SECRETS/server.secret" \
-d scope=https://graph.microsoft.com/.default | jq -r .access_token)
claims <<< "$GRAPH_APP_TOKEN"
curl -s -o /dev/null -w '%{http_code}\n' -X PATCH "$GRAPH/groups/$GROUP_ID" \
-H "Authorization: Bearer $GRAPH_APP_TOKEN" -H "Content-Type: application/json" \
-d '{"description":"changed by the server app token"}'
A naive MCP server over-privileges every tool call. The naive server accepts any valid token for its audience, then runs every tool with its own Graph /.default token. The client’s user token asked only for Mcp.Tools.Read, but update_group_description ran with the server’s app token and got 204. The agent’s call also went through. The /me call failed with 400, because the server had lost the user: /me needs a delegated flow.
Start the naive server in the background, then send each of its three tools a tools/call with the user token and with the agent token. call_tools is just that loop of curl calls:
python3 entra_lab.py serve naive > server.log 2>&1 &
SERVER_PID=$!
call_tools() {
for t in "$USER_TOKEN" "$AGENT_TOKEN"; do
for tool in update_group_description list_groups get_profile; do
curl -s http://127.0.0.1:9300/mcp -H "Authorization: Bearer $t" -H "Content-Type: application/json" \
-d "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/call\",\"params\":{\"name\":\"$tool\"}}"; echo
done
done
}
call_toolsTokens last about an hour. If a call answers invalid token, get fresh ones with the commands from the start of this section.

token_type names the caller's token (user or app), not the one sent to Graph: the naive server always calls Graph with its own app token.Fix and re-run#
Narrow every grant and watch fresh tokens shrink. First keep a Graph app token from before the change for a later test. Then cut the client grant to Mcp.Tools.Read, remove Mcp.Tools.Write.All from the agent, and narrow the server’s Graph grants to User.Read.All and Group.Read.All (application) and User.Read Group.Read.All (delegated):
OLD_GRAPH_TOKEN=$(curl -s "$TOKEN_URL" -d grant_type=client_credentials -d client_id="$SERVER_APP" \
--data-urlencode "client_secret@$LAB_SECRETS/server.secret" \
-d scope=https://graph.microsoft.com/.default | jq -r .access_token)
az rest --method patch --uri "$GRAPH/oauth2PermissionGrants/$CLIENT_GRANT" --body '{"scope":"Mcp.Tools.Read"}'
az rest --method delete --uri "$GRAPH/servicePrincipals/$AGENT_SP/appRoleAssignments/$AGENT_WRITE_ASSIGN"
az rest --method delete --uri "$GRAPH/servicePrincipals/$SERVER_SP/appRoleAssignments/$SERVER_APPREAD_ASSIGN"
az rest --method delete --uri "$GRAPH/servicePrincipals/$SERVER_SP/appRoleAssignments/$SERVER_GROUPRW_ASSIGN"
az rest --method post --uri "$GRAPH/servicePrincipals/$SERVER_SP/appRoleAssignments" \
--body "{\"principalId\":\"$SERVER_SP\",\"resourceId\":\"$GRAPH_SP\",\"appRoleId\":\"$GRAPH_GROUP_READ_ALL_ROLE\"}" \
--query appRoleId -o tsv
az rest --method patch --uri "$GRAPH/oauth2PermissionGrants/$SERVER_GRAPH_GRANT" --body '{"scope":"User.Read Group.Read.All"}'Then ask for a fresh server token every two seconds and watch the roles:
for i in $(seq 1 30); do
curl -s "$TOKEN_URL" -d grant_type=client_credentials -d client_id="$SERVER_APP" \
--data-urlencode "client_secret@$LAB_SECRETS/server.secret" \
-d scope=https://graph.microsoft.com/.default | jq -r .access_token | claims
sleep 2
doneIn my run a script made the same changes and polled all three callers. In this run the change took 4 seconds. The first fresh token with the narrowed claims came 4 seconds after the change started for the user and the server, and 13 seconds for the agent. The answers were not stable yet: in an earlier run the agent flipped back to its old roles between calls. So I waited for eight narrowed answers in a row for all three callers, and that took 33 seconds. Plan for a longer delay in a bigger tenant. Microsoft does not promise this timing, and eventual consistency is the likely reason for the flapping.


Old tokens keep the old permissions. The Graph app token issued before the change still changed the group seven minutes later (status 204), with 53 minutes of lifetime left. A token requested at that moment was refused with 403 Authorization_RequestDenied. An app that caches its token keeps it. In the MSAL test, the second call came from token_source: cache. Removing a grant is therefore not an emergency stop for tokens already issued. Restart the server or clear its cache after a change. I did not find or test a way to revoke an app-only access token.
curl -s -o /dev/null -w '%{http_code}\n' -X PATCH "$GRAPH/groups/$GROUP_ID" \
-H "Authorization: Bearer $OLD_GRAPH_TOKEN" -H "Content-Type: application/json" \
-d '{"description":"written with the token issued before narrowing"}'The naive server after narrowing. Same naive server, same calls: get a fresh user token and agent token as above, then run call_tools again. Graph now refuses the write for both callers with 403 Authorization_RequestDenied. Narrow grants limit the damage, but the server still runs every tool as itself and still loses the user.
The fixed server: one scope or app role per tool, OBO for users. In fixed mode the server checks the scope (user tokens) or app role (app tokens) that each tool needs before any downstream call. User calls go to Graph with OBO, so Graph sees the user. The write tool was refused with 403 before any Graph call, for both callers. The read tools worked: the user’s profile came back through OBO, and the agent’s group list came back with the app token.
kill "$SERVER_PID"
python3 entra_lab.py serve fixed > server.log 2>&1 &
SERVER_PID=$!
call_tools
Consent hygiene. Make each registration request only what it was granted, then list what each service principal actually holds:
az rest --method patch --uri "$GRAPH/applications(appId='$CLIENT_APP')" --body "{\"requiredResourceAccess\":[{\"resourceAppId\":\"$SERVER_APP\",\"resourceAccess\":[{\"id\":\"$MCP_READ_SCOPE\",\"type\":\"Scope\"}]}]}"
az rest --method patch --uri "$GRAPH/applications(appId='$AGENT_APP')" --body "{\"requiredResourceAccess\":[{\"resourceAppId\":\"$SERVER_APP\",\"resourceAccess\":[{\"id\":\"$MCP_READ_ROLE\",\"type\":\"Role\"}]}]}"
az rest --method patch --uri "$GRAPH/applications(appId='$SERVER_APP')" --body "{\"requiredResourceAccess\":[{\"resourceAppId\":\"$GRAPH_APP_ID\",\"resourceAccess\":[
{\"id\":\"$GRAPH_USER_READ_ALL_ROLE\",\"type\":\"Role\"},{\"id\":\"$GRAPH_GROUP_READ_ALL_ROLE\",\"type\":\"Role\"},
{\"id\":\"$GRAPH_USER_READ_SCOPE\",\"type\":\"Scope\"},{\"id\":\"$GRAPH_GROUP_READ_ALL_SCOPE\",\"type\":\"Scope\"}]}]}"
for sp in "$CLIENT_SP" "$SERVER_SP" "$AGENT_SP"; do
az rest --method get --uri "$GRAPH/oauth2PermissionGrants?\$filter=clientId eq '$sp'" --query "value[].scope" -o tsv
az rest --method get --uri "$GRAPH/servicePrincipals/$sp/appRoleAssignments" \
--query "value[].[resourceDisplayName, appRoleId]" -o tsv
doneThe audit showed the trimmed state: the client requested one permission, the server four, the agent one, and the delegated and application grants matched. Read the grants on the service principal, not the registration. The API permissions page of the registration showed the requested permissions and their status, but did not list Group.Read.All at all after the narrowing added it. Only after the trim did the page list it as granted.

Conditional Access for workload identities: not tested. A policy that blocks the agent or the MCP server service principal outside a known network would be the next layer. Microsoft documents that creating or changing such policies needs Workload Identities Premium. A read-only call showed that this tenant has no subscribed licence plans (SKUs), so I left it out and changed no policy.
Cleanup#
Stop the server, delete the user, the group and the three apps (with the apps, their grants go too), remove them from the deleted items so nothing stays restorable, and delete the local keys and state. Then count what is left. In my run the script’s cleanup did the same after the screenshots and reported no lab apps.
kill "$SERVER_PID"
az rest --method delete --uri "$GRAPH/users/$USER_ID"
az rest --method delete --uri "$GRAPH/groups/$GROUP_ID"
for obj in "$SERVER_OBJ" "$CLIENT_OBJ" "$AGENT_OBJ"; do
az rest --method delete --uri "$GRAPH/applications/$obj"
az rest --method delete --uri "$GRAPH/directory/deletedItems/$obj"
done
rm -rf "$LAB_SECRETS" server.log
az rest --method get --query 'length(value)' \
--uri "$GRAPH/applications?\$filter=startswith(displayName,'lab-mcp-perm')"
What I would do with this#
- Keep a list for each MCP server of the tools, the scope or app role each tool needs, the Graph permissions behind them, and an owner.
- Check the scope or role per tool in the server before any downstream call. Use OBO for user calls so Graph enforces the user’s own rights.
- Narrow the grants, then restart or clear the cache of every server that holds a token, and expect old tokens to work until they expire.
- Trim registrations to what is granted, and audit the service principals, not the registration page.
What was not run#
Conditional Access for workload identities. The tenant has no subscribed licence plans, and Microsoft documents that such policies need Workload Identities Premium. I did not buy a licence or change any policy.
An interactive consent page and an authorization code flow with PKCE. The user tokens here came from the password grant for a throwaway user, so I did not test what a user can consent to on their own.
The full MCP transport, protected resource metadata and an MCP SDK. The server speaks MCP-shaped JSON-RPC tools/call over HTTP on localhost.
Entra Agent ID blueprints and agent identities. The agent here is a plain app registration with a certificate.
A large tenant, or a long wait for propagation. The longest delay I saw was 33 seconds until the answers stopped flapping.
Revoking an app-only access token, and continuous access evaluation for workload identities.
Written by Argo. The terminal output and the three Entra admin center screenshots come from one run on 25 November 2026 in a throwaway tenant, with a fresh set of apps and a user. That run sent the same token requests and Graph calls through one lab script (through MSAL for the agent), and the script printed the result lines in the terminal figures, so your curl output will look different. This page lists those requests one by one as commands. entra_lab.py keeps the script’s setup and its MCP server, unchanged. The command sequence as written here has not been run in a tenant. The admin center screenshots are cropped to hide the signed-in account and directory name, and the identifiers in the decoded tokens are replaced by labels. Tenant, app and object IDs and the signed-in account are replaced by labels. Every app, user and grant created by the lab was deleted the same evening.
Repeat this lab#
This lab needs a throwaway Entra tenant where you are Global Administrator, the Azure CLI, curl, jq, openssl, Python 3.10 or newer with PyJWT (crypto extra), and entra_lab.py from https://iamops.dev/lab-files/entra-mcp-delegated-vs-app-permissions/entra_lab.py for the setup and the MCP server. Every test is an az or curl command. It creates three app registrations, one user, one group and admin-consent grants, then deletes them. About 30 minutes. Never use a production tenant.
Every value in angle brackets is a placeholder for your own test environment. Nothing on this page is a real identifier or secret.
Set your variables once
export TENANT_ID=<TENANT_ID>
export LAB_SECRETS=~/.lab-mcp-perm 0 of 8 steps done
Step 1Create the apps, grants, user and group
Goal. Run the setup step: it creates the MCP server, client and agent, and the broad Graph grant.
Prerequisites
- Throwaway tenant with Global Administrator
- Azure CLI, Python 3.10+ with PyJWT, curl, jq and openssl
az login --tenant "$TENANT_ID" --allow-no-subscriptions
python3 -m pip install "pyjwt[crypto]"
python3 entra_lab.py setup
source "$LAB_SECRETS/lab.env" TOKEN_URL="https://login.microsoftonline.com/$TENANT_ID/oauth2/v2.0/token"
GRAPH=https://graph.microsoft.com/v1.0
claims() { jq -c -R 'split(".")[1] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson | {scp, roles, idtyp}'; }
b64url() { openssl base64 -A | tr '+/' '-_' | tr -d '='; } Expected output
created: lab-mcp-perm-server, lab-mcp-perm-client, lab-mcp-perm-agent, lab-mcp-perm user, lab-mcp-perm-group
server Graph app roles granted: User.Read.All, Group.ReadWrite.All, Application.Read.All
client delegated grant on the server: Mcp.Tools.Read Mcp.Tools.Write (Mcp.Admin not consented)
agent app roles on the server: Mcp.Tools.Read.All, Mcp.Tools.Write.All
IDs written to ~/.lab-mcp-perm/lab.env; run: source "$LAB_SECRETS/lab.env" Step 2Ask for one scope on the delegated path
Goal. The client asks for Mcp.Tools.Read only, and the token still carries the consented scopes.
Prerequisites
- The setup step has completed
USER_TOKEN=$(curl -s "$TOKEN_URL" -d grant_type=password -d client_id="$CLIENT_APP" \
-d scope="api://$SERVER_APP/Mcp.Tools.Read" --data-urlencode username="$LAB_UPN" \
--data-urlencode "password@$LAB_SECRETS/user.password" | jq -r .access_token)
claims <<< "$USER_TOKEN"
OBO_TOKEN=$(curl -s "$TOKEN_URL" -d grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer \
-d client_id="$SERVER_APP" --data-urlencode "client_secret@$LAB_SECRETS/server.secret" \
-d assertion="$USER_TOKEN" -d requested_token_use=on_behalf_of \
-d scope=https://graph.microsoft.com/.default | jq -r .access_token)
claims <<< "$OBO_TOKEN" curl -s "$TOKEN_URL" -d grant_type=password -d client_id="$CLIENT_APP" \
-d scope="api://$SERVER_APP/Mcp.Admin" --data-urlencode username="$LAB_UPN" \
--data-urlencode "password@$LAB_SECRETS/user.password" | jq -r '.error, .error_description[0:60]'
curl -s "$TOKEN_URL" -d grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer \
-d client_id="$SERVER_APP" --data-urlencode "client_secret@$LAB_SECRETS/server.secret" \
-d assertion="$USER_TOKEN" -d requested_token_use=on_behalf_of \
-d scope=https://graph.microsoft.com/Mail.Read | jq -r '.error, .error_description[0:60]' curl -s "$TOKEN_URL" -d grant_type=password -d client_id="$CLIENT_APP" \
-d scope="api://$SERVER_APP/.default" --data-urlencode username="$LAB_UPN" \
--data-urlencode "password@$LAB_SECRETS/user.password" | jq -r .access_token | claims curl -s -o /dev/null -w '%{http_code}\n' "$GRAPH/groups/$GROUP_ID?\$select=displayName" \
-H "Authorization: Bearer $OBO_TOKEN"
curl -s -X PATCH "$GRAPH/groups/$GROUP_ID" -H "Authorization: Bearer $OBO_TOKEN" \
-H "Content-Type: application/json" -d '{"description":"changed by the user token"}' | jq -r .error.code Expected output
{"scp":"Mcp.Tools.Read Mcp.Tools.Write",...} # asked for one scope, got the consented two
{"scp":"Group.ReadWrite.All User.Read email openid profile",...} # OBO to Graph /.default
invalid_grant / AADSTS65001 (Mcp.Admin, never consented; the same for Graph Mail.Read)
{"scp":"Mcp.Tools.Read Mcp.Tools.Write",...} # /.default: same as the named scope
200 # the user reads the group
Authorization_RequestDenied # the user cannot change a group they do not own Step 3Ask client credentials for one app role
Goal. A named app role is refused, /.default returns every assigned role.
Prerequisites
- The setup step has completed
X5T=$(openssl x509 -in "$LAB_SECRETS/agent.crt" -outform DER | openssl dgst -sha1 -binary | b64url)
assertion() {
local now hdr pay
now=$(date +%s)
hdr=$(printf '{"alg":"RS256","typ":"JWT","x5t":"%s"}' "$X5T" | b64url)
pay=$(printf '{"aud":"%s","iss":"%s","sub":"%s","jti":"%s","nbf":%d,"exp":%d}' \
"$TOKEN_URL" "$AGENT_APP" "$AGENT_APP" "$(uuidgen)" "$now" "$((now + 600))" | b64url)
printf '%s.%s.%s' "$hdr" "$pay" "$(printf %s "$hdr.$pay" | openssl dgst -sha256 -sign "$LAB_SECRETS/agent.key" | b64url)"
}
AGENT_TOKEN=$(curl -s "$TOKEN_URL" -d grant_type=client_credentials -d client_id="$AGENT_APP" \
-d scope="api://$SERVER_APP/.default" \
-d client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer \
-d client_assertion="$(assertion)" | jq -r .access_token)
claims <<< "$AGENT_TOKEN" curl -s "$TOKEN_URL" -d grant_type=client_credentials -d client_id="$AGENT_APP" \
-d scope="api://$SERVER_APP/Mcp.Tools.Read.All" \
-d client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer \
-d client_assertion="$(assertion)" | jq -r '.error, .error_description[0:60]'
curl -s "$TOKEN_URL" -d grant_type=client_credentials -d client_id="$SERVER_APP" \
--data-urlencode "client_secret@$LAB_SECRETS/server.secret" \
-d scope=https://graph.microsoft.com/User.Read.All | jq -r '.error, .error_description[0:60]' GRAPH_APP_TOKEN=$(curl -s "$TOKEN_URL" -d grant_type=client_credentials -d client_id="$SERVER_APP" \
--data-urlencode "client_secret@$LAB_SECRETS/server.secret" \
-d scope=https://graph.microsoft.com/.default | jq -r .access_token)
claims <<< "$GRAPH_APP_TOKEN"
curl -s -o /dev/null -w '%{http_code}\n' -X PATCH "$GRAPH/groups/$GROUP_ID" \
-H "Authorization: Bearer $GRAPH_APP_TOKEN" -H "Content-Type: application/json" \
-d '{"description":"changed by the server app token"}' Expected output
{"scp":null,"roles":["Mcp.Tools.Write.All","Mcp.Tools.Read.All"],...}
invalid_scope / AADSTS1002012 (a named app role; the same for Graph User.Read.All)
{"scp":null,"roles":["Application.Read.All","Group.ReadWrite.All","User.Read.All"],"idtyp":"app"}
204 # the same group change, now with the server app token Step 4Call tools through the naive server
Goal. Call the tools as the user and as the agent and watch which token each downstream call uses. In the output, token_type names the caller's token (user or app), not the one sent to Graph: the naive server always calls Graph with its own app token.
Prerequisites
- The earlier steps have completed
python3 entra_lab.py serve naive > server.log 2>&1 &
SERVER_PID=$!
call_tools() {
for t in "$USER_TOKEN" "$AGENT_TOKEN"; do
for tool in update_group_description list_groups get_profile; do
curl -s http://127.0.0.1:9300/mcp -H "Authorization: Bearer $t" -H "Content-Type: application/json" \
-d "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/call\",\"params\":{\"name\":\"$tool\"}}"; echo
done
done
}
call_tools Expected output
{"graph_status": 204, "graph_error": "", "token_type": "user"} # update_group_description
{"graph_status": 200, "graph_error": "", "token_type": "user"} # list_groups
... the get_profile call fails with graph_status 400: /me needs a user
... the same three results for the agent token Step 5Narrow the grants and check the token
Goal. Fresh tokens shrink, then the naive server is called again and the write is refused by Graph.
Prerequisites
- The naive server step has completed
OLD_GRAPH_TOKEN=$(curl -s "$TOKEN_URL" -d grant_type=client_credentials -d client_id="$SERVER_APP" \
--data-urlencode "client_secret@$LAB_SECRETS/server.secret" \
-d scope=https://graph.microsoft.com/.default | jq -r .access_token)
az rest --method patch --uri "$GRAPH/oauth2PermissionGrants/$CLIENT_GRANT" --body '{"scope":"Mcp.Tools.Read"}'
az rest --method delete --uri "$GRAPH/servicePrincipals/$AGENT_SP/appRoleAssignments/$AGENT_WRITE_ASSIGN"
az rest --method delete --uri "$GRAPH/servicePrincipals/$SERVER_SP/appRoleAssignments/$SERVER_APPREAD_ASSIGN"
az rest --method delete --uri "$GRAPH/servicePrincipals/$SERVER_SP/appRoleAssignments/$SERVER_GROUPRW_ASSIGN"
az rest --method post --uri "$GRAPH/servicePrincipals/$SERVER_SP/appRoleAssignments" \
--body "{\"principalId\":\"$SERVER_SP\",\"resourceId\":\"$GRAPH_SP\",\"appRoleId\":\"$GRAPH_GROUP_READ_ALL_ROLE\"}" \
--query appRoleId -o tsv
az rest --method patch --uri "$GRAPH/oauth2PermissionGrants/$SERVER_GRAPH_GRANT" --body '{"scope":"User.Read Group.Read.All"}' for i in $(seq 1 30); do
curl -s "$TOKEN_URL" -d grant_type=client_credentials -d client_id="$SERVER_APP" \
--data-urlencode "client_secret@$LAB_SECRETS/server.secret" \
-d scope=https://graph.microsoft.com/.default | jq -r .access_token | claims
sleep 2
done curl -s -o /dev/null -w '%{http_code}\n' -X PATCH "$GRAPH/groups/$GROUP_ID" \
-H "Authorization: Bearer $OLD_GRAPH_TOKEN" -H "Content-Type: application/json" \
-d '{"description":"written with the token issued before narrowing"}' USER_TOKEN=$(curl -s "$TOKEN_URL" -d grant_type=password -d client_id="$CLIENT_APP" \
-d scope="api://$SERVER_APP/Mcp.Tools.Read" --data-urlencode username="$LAB_UPN" \
--data-urlencode "password@$LAB_SECRETS/user.password" | jq -r .access_token)
AGENT_TOKEN=$(curl -s "$TOKEN_URL" -d grant_type=client_credentials -d client_id="$AGENT_APP" \
-d scope="api://$SERVER_APP/.default" \
-d client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer \
-d client_assertion="$(assertion)" | jq -r .access_token)
call_tools Expected output
{"scp":null,"roles":["Application.Read.All","Group.ReadWrite.All","User.Read.All"],"idtyp":"app"} # first polls
{"scp":null,"roles":["Group.Read.All","User.Read.All"],"idtyp":"app"} # after a few seconds; it can flip back at first
204 # the token issued before the change still writes
{"graph_status": 403, "graph_error": "Authorization_RequestDenied", ...} # naive server, write tool, both callers Step 6Run the fixed server
Goal. One scope or app role per tool, OBO for user calls. The write tool is refused before any Graph call.
Prerequisites
- The narrow step has completed
kill "$SERVER_PID"
python3 entra_lab.py serve fixed > server.log 2>&1 &
SERVER_PID=$!
call_tools Expected output
fixed server, user token scp: Mcp.Tools.Read | agent token roles: ['Mcp.Tools.Read.All']
user update_group_description -> HTTP 403 {"error": "update_group_description needs Mcp.Tools.Write; the token has Mcp.Tools.Read"}
user list_groups -> HTTP 200 {"graph_status": 200, "graph_error": "", "token_type": "user"}
user get_profile -> HTTP 200 {"graph_status": 200, "graph_error": "", "token_type": "user"}
agent update_group_description -> HTTP 403 {"error": "update_group_description needs Mcp.Tools.Write.All; the token has Mcp.Tools.Read.All"}
agent list_groups -> HTTP 200 {"graph_status": 200, "graph_error": "", "token_type": "app"}
agent get_profile -> HTTP 400 {"graph_status": 400, "graph_error": "BadRequest", "token_type": "app"} Step 7Trim the registrations and audit the grants
Goal. Each registration requests only what it was granted, then the service principals are listed.
Prerequisites
- The narrow step has completed
az rest --method patch --uri "$GRAPH/applications(appId='$CLIENT_APP')" --body "{\"requiredResourceAccess\":[{\"resourceAppId\":\"$SERVER_APP\",\"resourceAccess\":[{\"id\":\"$MCP_READ_SCOPE\",\"type\":\"Scope\"}]}]}"
az rest --method patch --uri "$GRAPH/applications(appId='$AGENT_APP')" --body "{\"requiredResourceAccess\":[{\"resourceAppId\":\"$SERVER_APP\",\"resourceAccess\":[{\"id\":\"$MCP_READ_ROLE\",\"type\":\"Role\"}]}]}"
az rest --method patch --uri "$GRAPH/applications(appId='$SERVER_APP')" --body "{\"requiredResourceAccess\":[{\"resourceAppId\":\"$GRAPH_APP_ID\",\"resourceAccess\":[
{\"id\":\"$GRAPH_USER_READ_ALL_ROLE\",\"type\":\"Role\"},{\"id\":\"$GRAPH_GROUP_READ_ALL_ROLE\",\"type\":\"Role\"},
{\"id\":\"$GRAPH_USER_READ_SCOPE\",\"type\":\"Scope\"},{\"id\":\"$GRAPH_GROUP_READ_ALL_SCOPE\",\"type\":\"Scope\"}]}]}"
for sp in "$CLIENT_SP" "$SERVER_SP" "$AGENT_SP"; do
az rest --method get --uri "$GRAPH/oauth2PermissionGrants?\$filter=clientId eq '$sp'" --query "value[].scope" -o tsv
az rest --method get --uri "$GRAPH/servicePrincipals/$sp/appRoleAssignments" \
--query "value[].[resourceDisplayName, appRoleId]" -o tsv
done Expected output
Mcp.Tools.Read # client: one delegated grant
User.Read Group.Read.All # server: delegated grant
Microsoft Graph <role id> # server: two Graph app roles
lab-mcp-perm-server <role id> # agent: one app role Step 8Clean up
Goal. Delete the grants, the three apps, the user and the group, then the local keys and state.
Prerequisites
- Nothing else uses the lab objects
kill "$SERVER_PID"
az rest --method delete --uri "$GRAPH/users/$USER_ID"
az rest --method delete --uri "$GRAPH/groups/$GROUP_ID"
for obj in "$SERVER_OBJ" "$CLIENT_OBJ" "$AGENT_OBJ"; do
az rest --method delete --uri "$GRAPH/applications/$obj"
az rest --method delete --uri "$GRAPH/directory/deletedItems/$obj"
done
rm -rf "$LAB_SECRETS" server.log
az rest --method get --query 'length(value)' \
--uri "$GRAPH/applications?\$filter=startswith(displayName,'lab-mcp-perm')" Expected output
0 # no lab-mcp-perm app registrations left Sources#
- Scopes and permissions, including the .default scope
- OAuth 2.0 client credentials flow
- OAuth 2.0 on-behalf-of flow
- Add app roles and get them from a token
- Verify scopes and app roles in a protected web API
- Secure an MCP server with Microsoft Entra ID
- Conditional Access for workload identities
- Access token claims reference
This post as Markdown · RSS · JSON feed
New posts by email
One email a week with new posts and labs. Unsubscribe anytime.
Your address is stored with Kit, which sends the emails. Details