Skip to content

feat(context): add whoami action for token introspection (#203)#205

Merged
polaz merged 7 commits intomainfrom
feat/#203-featcontext-add-whoami-action-for-token-introspect
Jan 25, 2026
Merged

feat(context): add whoami action for token introspection (#203)#205
polaz merged 7 commits intomainfrom
feat/#203-featcontext-add-whoami-action-for-token-introspect

Conversation

@polaz
Copy link
Copy Markdown
Member

@polaz polaz commented Jan 25, 2026

Summary

Add new whoami action to manage_context tool that provides comprehensive information about current authentication status, token capabilities, and available tools. This enables AI agents to understand access limitations and provide actionable guidance to users.

Key Feature: Dynamic Token Refresh - When called, whoami re-introspects the token to detect permission changes. If scopes have changed since startup, the tool registry is automatically refreshed and clients receive a tools/list_changed notification. No server restart required.

Problem

When a user connects with a limited-scope token, the MCP server silently filters tools and the agent has no way to understand why operations fail or how to help the user fix access issues. Additionally, if the user updates their token permissions, they previously had to restart the server.

Solution

The whoami action returns detailed introspection data:

  • User identity — fetched via REST API (works with any token scope)
  • Token info — name, scopes, expiry, validity status
  • Server config — host, version, tier (free/premium/ultimate)
  • Capabilities summary — available/filtered tools with breakdown by filter reason
  • Current context — active preset, profile, scope
  • Warnings — expiring token, limited scopes, read-only mode, tier restrictions
  • Recommendations — actionable guidance with URLs for token creation
  • scopesRefreshed — indicates if token permissions changed and tools were updated

Dynamic Token Refresh Flow

User: "I added the api scope to my token"
Agent: [calls manage_context { action: "whoami" }]

→ whoami re-introspects token via GitLab API
→ Detects scopes changed from ["read_user"] to ["api", "read_user"]  
→ Refreshes RegistryManager tool cache
→ Sends tools/list_changed notification to clients
→ Returns { scopesRefreshed: true, ... }

Agent: "Your token scopes have been updated! You now have access to 45 tools."

Changes

  • Added WhoamiSchema to discriminated union in schema.ts
  • Added comprehensive types in types.ts (WhoamiResult, WhoamiUserInfo, WhoamiTokenInfo, etc.)
  • New src/entities/context/whoami.ts with main implementation
  • Added refreshTokenScopes() to ConnectionManager for hot-reload
  • Added getFilterStats() to RegistryManager for tool filtering statistics
  • Updated handlers.ts with handleWhoami() dispatcher
  • Integrated sendToolsListChangedNotification() for client updates
  • 34 unit tests covering various token scenarios

Example Response

{
  "user": {
    "id": 123,
    "username": "developer",
    "name": "John Developer",
    "state": "active"
  },
  "token": {
    "type": "personal_access_token",
    "name": "gitlab-mcp",
    "scopes": ["api", "read_user"],
    "expiresAt": "2025-12-31",
    "daysUntilExpiry": 340,
    "isValid": true,
    "hasGraphQLAccess": true,
    "hasWriteAccess": true
  },
  "server": {
    "host": "gitlab.example.com",
    "version": "17.5.2",
    "tier": "ultimate",
    "readOnlyMode": false
  },
  "capabilities": {
    "canBrowse": true,
    "canManage": true,
    "canAccessGraphQL": true,
    "availableToolCount": 45,
    "totalToolCount": 45,
    "filteredByScopes": 0
  },
  "warnings": [],
  "recommendations": [],
  "scopesRefreshed": false
}

Test plan

  • yarn lint passes (0 errors)
  • yarn test passes (4005 tests)
  • yarn build succeeds
  • Manual testing with limited-scope token
  • Manual testing with expired token
  • Manual testing: add scope to token, verify hot-reload works

Closes #203

Copilot AI review requested due to automatic review settings January 25, 2026 03:58
@github-actions
Copy link
Copy Markdown

github-actions bot commented Jan 25, 2026

Test Coverage Report

Overall Coverage: 93.6%

Metric Percentage
Statements 93.08%
Branches 84.89%
Functions 83.1%
Lines 93.6%

View detailed coverage report

@codecov
Copy link
Copy Markdown

codecov bot commented Jan 25, 2026

Codecov Report

❌ Patch coverage is 91.66667% with 14 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/entities/context/whoami.ts 92.92% 3 Missing and 5 partials ⚠️
src/registry-manager.ts 82.85% 5 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new whoami action to the manage_context tool, enabling AI agents to introspect authentication status, token capabilities, and tool availability. This solves a critical UX problem where agents couldn't understand or communicate why operations fail due to limited token scopes.

Changes:

  • Adds comprehensive token introspection with user identity, token info, server config, capabilities breakdown, and actionable recommendations
  • Implements getFilterStats() method in RegistryManager to provide detailed tool filtering statistics
  • Includes 23 unit tests covering various token scenarios (full access, limited scopes, expired tokens, OAuth mode, read-only mode, tier restrictions)

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/entities/context/schema.ts Adds WhoamiSchema to discriminated union, updates action count to 8
src/entities/context/types.ts Defines comprehensive type interfaces for whoami response (WhoamiResult, WhoamiUserInfo, WhoamiTokenInfo, etc.)
src/entities/context/whoami.ts Core implementation with user fetching, token/server info building, capability analysis, and recommendation generation
src/entities/context/handlers.ts Adds handleWhoami dispatcher and updates return type union
src/entities/context/registry.ts Updates tool description to include whoami action
src/registry-manager.ts Adds getFilterStats() method with FilterStats interface to expose tool filtering statistics
tests/unit/entities/context/whoami.test.ts Comprehensive test suite with 23 tests covering multiple scenarios

…ty discovery

Add new `whoami` action to manage_context tool that provides comprehensive
information about current authentication status, token capabilities, and
available tools. This enables AI agents to understand access limitations
and provide actionable guidance to users.

Key features:
- User identity info (fetched via REST API)
- Token info (name, scopes, expiry, validity)
- Server config (host, version, tier)
- Capabilities summary (available/filtered tools breakdown)
- Current context (preset, profile, scope)
- Warnings (expiring token, limited scopes, read-only mode)
- Recommendations (create token, renew, upgrade tier)

Dynamic Token Refresh:
- Re-introspects token on each whoami call
- Detects permission changes without server restart
- Automatically refreshes tool registry when scopes change
- Sends tools/list_changed notification to MCP clients
- Returns scopesRefreshed=true when tools were updated

Implementation:
- Add WhoamiSchema to discriminated union schema
- Add comprehensive types (WhoamiResult, WhoamiUserInfo, etc.)
- New whoami.ts with main logic and token refresh flow
- Add refreshTokenScopes() to ConnectionManager
- Add getFilterStats() to RegistryManager with FilterStats interface
- Integrate sendToolsListChangedNotification for hot-reload
- 34 unit tests covering various scenarios

Author: Pavel Oliynyk <[email protected]>

Closes #203
@polaz polaz force-pushed the feat/#203-featcontext-add-whoami-action-for-token-introspect branch from bc2b08a to 86a307e Compare January 25, 2026 04:15
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

- Replace local isOAuthMode() with shared isOAuthEnabled()
- Add tests for ConnectionManager.refreshTokenScopes method
- Add isOAuthEnabled mock to whoami tests
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

polaz added 3 commits January 25, 2026 06:42
- Remove @author tag to follow codebase conventions
- Simplify scope assignment using nullish coalescing
- Remove unused RuntimeScope import
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Set filteredByScopes to 35 to reflect that write_repository
scope without api filters most tools requiring GraphQL access.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated no new comments.

@polaz polaz merged commit fc0c568 into main Jan 25, 2026
27 checks passed
@polaz polaz deleted the feat/#203-featcontext-add-whoami-action-for-token-introspect branch January 25, 2026 12:14
sw-release-bot bot pushed a commit that referenced this pull request Jan 25, 2026
## [6.46.0](v6.45.1...v6.46.0) (2026-01-25)

### Features

* **context:** add whoami action for token introspection ([#203](#203)) ([#205](#205)) ([fc0c568](fc0c568))
@sw-release-bot
Copy link
Copy Markdown

🎉 This PR is included in version 6.46.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(context): Add 'whoami' action for token introspection and capability discovery

2 participants