Skip to content

fix: add sdpm-mcp/invoke scope to WebClient OAuth config#112

Merged
okamoto-aws merged 2 commits intomainfrom
fix/webclient-mcp-scope
May 3, 2026
Merged

fix: add sdpm-mcp/invoke scope to WebClient OAuth config#112
okamoto-aws merged 2 commits intomainfrom
fix/webclient-mcp-scope

Conversation

@okamoto-aws
Copy link
Copy Markdown
Contributor

@okamoto-aws okamoto-aws commented May 2, 2026

Summary

PR #83 introduced scope-based auth for the MCP Server (allowedScopes: ["sdpm-mcp/invoke"]), but did not add that scope to the WebClient's allowed OAuth scopes. As a result, tokens issued to WebUI users are missing sdpm-mcp/invoke, and Agent → MCP Server requests fail with 401 Unauthorized. Chat stops streaming and the user sees a perpetual loading indicator.

This PR adds sdpm-mcp/invoke to the WebClient via the existing UpdateCognitoCallbackUrls custom resource in WebUiStack. This approach avoids any changes to AuthStack's UserPoolClient construct — which matters because modifying oAuth.scopes on the CDK construct triggers a CloudFormation UserPoolClient replacement, and the client's ID is exported via auto-generated Fn::ImportValue to SdpmAgent / SdpmRuntime / SdpmWebUi. A replacement would fail with "Cannot delete export" on any in-place upgrade.

Changes

File Change
infra/lib/web-ui-stack.ts Add mcpCustomScope prop; include it in AllowedOAuthScopes of the UpdateCognitoCallbackUrls custom resource; append it to the scope field written to aws-exports.json so that the browser OIDC library explicitly requests it on token refresh
infra/bin/infra.ts Pass authStack.mcpCustomScope to WebUiStack

AuthStack is intentionally left untouched. The Cognito Resource Server and the WebClient itself are not modified — only the AllowedOAuthScopes value on the existing client is updated via a Cognito API call, which is an in-place update with no replacement.

Why custom resource instead of CDK oAuth.scopes

Changing oAuth.scopes on userPool.addClient(...) causes CDK to emit a UserPoolClient replacement. CloudFormation then tries to delete the outgoing client's auto-generated export (SdpmAuth:ExportsOutputRefUserPoolWebClient...), which is currently imported by three downstream stacks. The delete fails, and the entire Auth stack rolls back. The custom resource route sidesteps that by leaving CloudFormation resource definitions unchanged and just patching the live Cognito client.

Verification

  • npx tsc --noEmit passes
  • Deployed to us-east-1 via bash scripts/deploy.sh — all stacks CREATE_COMPLETE / UPDATE_COMPLETE
  • Verified post-deploy that the WebClient now has sdpm-mcp/invoke in its allowed scopes
  • Users must sign out, clear site data, and sign back in once after the fix rolls out so their token is reissued with the new scope

Follow-up

There is a broader refactor being considered to stop using auto-generated CloudFormation exports for cross-stack references and migrate to SSM Parameter Store instead. That work will be tracked in a separate PR (#113). The current PR is deliberately minimal and only fixes the immediate 401 regression.

@okamoto-aws okamoto-aws added blog:skip ブログ対象外 bug Something isn't working labels May 2, 2026
@okamoto-aws okamoto-aws force-pushed the fix/webclient-mcp-scope branch from 651f738 to c67889a Compare May 3, 2026 01:45
WebClient's access token lacked sdpm-mcp/invoke scope, causing Agent to
MCP Server requests to fail with 401 Unauthorized after PR #83.

Scope is added via the UpdateCognitoCallbackUrls custom resource in
web-ui-stack.ts, not via CDK's addClient oAuth.scopes. This avoids
Cognito UserPoolClient replacement (which would break existing
cross-stack exports) and keeps the change safe for in-place upgrades
of already-deployed environments.

- web-ui-stack.ts: add sdpm-mcp/invoke to AllowedOAuthScopes and to
  aws-exports.json scope field
- infra.ts: pass authStack.mcpCustomScope to WebUiStack
@okamoto-aws okamoto-aws force-pushed the fix/webclient-mcp-scope branch from c67889a to 13c37e1 Compare May 3, 2026 02:09
@okamoto-aws okamoto-aws merged commit f7fbdaa into main May 3, 2026
11 checks passed
@okamoto-aws okamoto-aws deleted the fix/webclient-mcp-scope branch May 3, 2026 02:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blog:skip ブログ対象外 bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant