Skip to content

feat(auth): scoped agent tokens for autonomous AI agents [#028]#319

Merged
Dumbris merged 16 commits intomainfrom
028-agent-tokens
Mar 6, 2026
Merged

feat(auth): scoped agent tokens for autonomous AI agents [#028]#319
Dumbris merged 16 commits intomainfrom
028-agent-tokens

Conversation

@Dumbris
Copy link
Contributor

@Dumbris Dumbris commented Mar 6, 2026

Summary

Implements scoped agent tokens that allow autonomous AI agents to access MCPProxy with restricted server scope and permission tiers. This is a complete feature spanning backend, CLI, REST API, and Web UI.

  • Token model: mcp_agt_ prefix, HMAC-SHA256 hashing with server-side key, mandatory expiry (max 365d)
  • Scope enforcement: Tokens restrict which upstream servers and permission tiers (read/write/destructive) an agent can access
  • REST API: Full CRUD at /api/v1/tokens — create, list, get, revoke, regenerate
  • CLI: mcpproxy token {create,list,show,revoke,regenerate} commands
  • MCP integration: retrieve_tools filters results to scoped servers; call_tool_* blocks out-of-scope access
  • Activity logging: Agent identity (name, token prefix) injected into activity records with --agent and --auth-type CLI filters
  • Web UI: Full token management page with create dialog, token list, revoke/regenerate actions, copy-to-clipboard for secrets

Key Design Decisions

  • HMAC-SHA256 for O(1) token lookup (not bcrypt — tokens have 256-bit entropy)
  • BBolt dual-bucket storage: agent_tokens (hash→JSON) + agent_token_names (name→hash)
  • Backward compatible: MCP endpoints default to admin context when no token is provided
  • File-based HMAC key at ~/.mcpproxy/.token_key (0600 permissions)

Files Changed (39 files, +7360/-22)

Area Files
Auth core internal/auth/agent_token.go, context.go + tests
Storage internal/storage/agent_tokens.go + tests
HTTP API internal/httpapi/tokens.go, server.go + tests
MCP scope internal/server/mcp.go + scope/activity tests
CLI cmd/mcpproxy/token_cmd.go + tests
Frontend AgentTokens.vue, SidebarNav.vue, router/index.ts, api.ts
Specs specs/028-agent-tokens/ (spec, plan, research, data-model, contracts, tasks)

Test plan

  • go test -race ./internal/auth/... ./internal/storage/... ./internal/httpapi/... ./internal/server/... ./cmd/mcpproxy/... — all pass
  • ./scripts/run-linter.sh — 0 issues
  • make build — frontend + backend build succeeds
  • Unit tests: 18 auth tests, 19 storage tests, 27 REST API tests, 14 middleware tests, 10 MCP scope tests, 7 activity metadata tests, 7 CLI tests
  • Manual: Create token via CLI, use with MCP, verify scope filtering
  • Manual: Web UI token management page

🤖 Generated with Claude Code

claude added 9 commits March 6, 2026 09:49
Design and speckit artifacts for scoped agent tokens feature.
Agent tokens allow autonomous AI agents to access MCPProxy with
restricted server access, permission tiers, and automatic expiry.

## Artifacts
- Design doc: docs/plans/2026-03-06-agent-tokens-design.md
- Teams auth design: docs/plans/2026-03-06-mcpproxy-teams-auth-design.md
- Spec: specs/028-agent-tokens/spec.md (6 user stories, 20 FRs)
- Plan: specs/028-agent-tokens/plan.md
- Research: specs/028-agent-tokens/research.md
- Data model: specs/028-agent-tokens/data-model.md
- API contracts: specs/028-agent-tokens/contracts/agent-tokens-api.yaml
- Tasks: specs/028-agent-tokens/tasks.md (43 tasks across 8 phases)
Add the internal/auth package with token generation, HMAC-SHA256
hashing, format validation, permission constants, AuthContext for
request-scoped identity propagation, and file-based HMAC key
management. Add BBolt storage layer with dual-bucket design
(hash->record, name->hash) supporting CRUD, revocation, regeneration,
last-used tracking, and token validation with expiry/revocation checks.

Includes 37 passing tests covering all functionality with race
detection clean.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…Phase 3+4, T010-T022)

Phase 3 (Auth Middleware):
- Add AuthContext injection in apiKeyAuthMiddleware for admin/agent token auth
- Add mcpAuthMiddleware for MCP endpoint agent token scope enforcement
- Support agent token validation via mcp_agt_ prefix in X-API-Key header
- Wire ExtractToken helper for unified token extraction from headers/query params
- Tray connections automatically get admin AuthContext

Phase 4 (REST API Token Management):
- Create internal/httpapi/tokens.go with 5 REST handlers:
  - POST /api/v1/tokens (create with name/permissions/servers/expiry validation)
  - GET /api/v1/tokens (list without secrets)
  - GET /api/v1/tokens/{name} (get single token info)
  - DELETE /api/v1/tokens/{name} (revoke)
  - POST /api/v1/tokens/{name}/regenerate (regenerate secret)
- All endpoints reject agent token auth with 403
- TokenStore interface for testable storage abstraction
- Validation helpers: name regex, permissions, expiry parsing (max 365d), server names
- Wire storage via SetTokenStore() in server initialization
- Register routes in setupRoutes() under /api/v1/tokens

Tests (27 test functions):
- Token CRUD lifecycle tests (create, list, get, revoke, regenerate)
- Validation: name format, permissions, expiry duration, allowed servers
- Security: agent token rejection (403), admin access, no-store handling (500)
- Validation helper unit tests (name, expiry, allowed servers)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
… (Phase 3 completion)

Add token CLI subcommands (create/list/show/revoke) and test suites for:
- Auth middleware: token extraction priority, agent token validation
  (valid/expired/revoked/Bearer), admin context propagation, tray bypass
- MCP scope enforcement: server access blocking, permission tier checks
  (read/write/destructive), admin passthrough, upstream server list
  filtering, quarantine security blocking for agent tokens

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add `mcpproxy token regenerate <name>` CLI command that calls
POST /api/v1/tokens/{name}/regenerate to invalidate the old secret
and generate a new one. Displays the new token with a save warning,
supports -o json output. Includes test verifying command registration
and argument validation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…e 6, T028-T031)

Add auth identity tracking to activity records so tool calls can be
attributed to specific agent tokens. Includes:

- getAuthMetadata/injectAuthMetadata helpers in mcp.go that extract
  auth context and inject _auth_ prefixed fields into activity args
- Auth metadata injected in handleRetrieveTools, handleCallToolVariant,
  and legacy handleCallTool before any activity emit calls
- AgentName and AuthType filters on ActivityFilter (storage + httpapi)
- CLI --agent and --auth-type flags on activity list command
- Swagger annotations for new query parameters
- Unit tests for getAuthMetadata and injectAuthMetadata functions

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add complete web UI for managing agent tokens:
- Token API methods in api.ts (list, create, revoke, regenerate)
- AgentTokens.vue view with stats bar, table, create dialog, and
  token secret display with copy-to-clipboard
- Route at /tokens and sidebar navigation entry
- TypeScript types for AgentTokenInfo, CreateAgentTokenRequest,
  CreateAgentTokenResponse

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Mar 6, 2026

Deploying mcpproxy-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: a231680
Status: ✅  Deploy successful!
Preview URL: https://ff3008ad.mcpproxy-docs.pages.dev
Branch Preview URL: https://028-agent-tokens.mcpproxy-docs.pages.dev

View logs

claude added 6 commits March 6, 2026 14:13
Covers motivation, quick start, permission tiers, server scoping,
require_mcp_auth enforcement, token management CLI/API, activity
logging integration, and security model.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…cation

When enabled, the /mcp endpoint rejects unauthenticated requests with 401.
Tray/socket connections always bypass this check (OS-level auth). Adds CLI
flag --require-mcp-auth and config field require_mcp_auth (default: false).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…responses

Replace text input for allowed_servers with a checkbox list showing all
configured servers with connected/offline badges, plus an "All servers"
wildcard option. Fix token API handlers to wrap responses in the standard
{success, data} envelope expected by the frontend.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Tests now unwrap the {success, data} envelope before asserting on
response fields, matching the writeSuccess/NewSuccessResponse changes.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add Auth Type filter (Admin/Agent) and Agent Name filter to Activity Log
- Agent name dropdown auto-populates from activity metadata
- Fix token secret display: use bg-neutral for dark theme visibility
- Fix Copy button: use btn-neutral for dark theme contrast
- Fix revoke: handle 204 No Content response in API client

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Windows does not support Unix file permissions, so os.FileMode(0600)
assertion always fails. Skip this check on Windows.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions
Copy link

github-actions bot commented Mar 6, 2026

📦 Build Artifacts

Workflow Run: View Run
Branch: 028-agent-tokens

Available Artifacts

  • archive-darwin-amd64 (24 MB)
  • archive-darwin-arm64 (21 MB)
  • archive-linux-amd64 (13 MB)
  • archive-linux-arm64 (11 MB)
  • archive-windows-amd64 (23 MB)
  • archive-windows-arm64 (21 MB)
  • frontend-dist-pr (0 MB)
  • installer-dmg-darwin-amd64 (26 MB)
  • installer-dmg-darwin-arm64 (24 MB)

How to Download

Option 1: GitHub Web UI (easiest)

  1. Go to the workflow run page linked above
  2. Scroll to the bottom "Artifacts" section
  3. Click on the artifact you want to download

Option 2: GitHub CLI

gh run download 22767352346 --repo smart-mcp-proxy/mcpproxy-go

Note: Artifacts expire in 14 days.

Keep design docs as local-only uncommitted files.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Dumbris Dumbris merged commit 522a9e9 into main Mar 6, 2026
41 checks passed
@Dumbris Dumbris deleted the 028-agent-tokens branch March 6, 2026 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants