Skip to content

feat(profiles): set_profile MCP tool + session resolver + REST /api/v1/profiles (Profiles v2 T2, MCP-3241) - #761

Merged
github-actions[bot] merged 4 commits into
mainfrom
feat/mcp-3241-profiles-v2-t2
Jun 24, 2026
Merged

github-actions[bot] merged 4 commits into
mainfrom
feat/mcp-3241-profiles-v2-t2

Conversation

@Dumbris

@Dumbris Dumbris commented Jun 24, 2026

Copy link
Copy Markdown
Member

Profiles v2 — T2 (MCP-3241)

Builds on T1 (per-profile Bleve indexes, #756). Adds stateful, in-session profile switching plus a REST surface for UI.

What's in

  • set_profile MCP tool (internal/server/profile_tool.go): {profile} → validates the slug, records it on the session, returns {active_profile, servers}; empty "" clears back to all servers. Registered on the default, call-tool and code-exec servers.
  • Session-scoped state: a mutex-guarded map[sessionID]profileName on SessionStore, cleared on session close (covers both the unregister hook and inactivity cleanup). First subtask confirmed: mcp-go exposes a stable per-session id at tool-call time via server.ClientSessionFromContext(ctx).SessionID() — no synthetic fallback needed.
  • Shared resolver (internal/server/profile_resolver.go) with precedence (highest wins): agent-token profile_pin (T3 hook, inert) > URL /mcp/p/<slug> (per-request override) > set_profile session selection > none. retrieve_tools, call_tool_*, code_execution, and activity metadata all consult it; retrieve_tools searches the per-profile index (no re-index on switch), with the shared index + scope post-filter as the allow-all fallback.
  • REST /api/v1/profiles (internal/httpapi/profiles.go): GET lists {name, servers, tool_count}; GET/PUT /profiles/active read/write a UI-facing server default. oas/swagger.yaml regenerated.
  • Docs: docs/features/profiles.md documents set_profile, the precedence table, and the REST surface.

Tests

  • profile_resolver_test.go: precedence (URL > session > none) + stale-selection cleanup + SessionStore lifecycle.
  • profile_integration_test.go: e2e on base /mcp — set_profile(research) scopes retrieve_tools, switch to deploy, clear restores all; unknown-slug rejection.
  • profiles_test.go: REST list (tool counts) + active get/put round-trip + 404/400.

Verification

  • go test -race ./internal/server/... ./internal/httpapi/... green (profile suite + full httpapi).
  • golangci-lint v2 (.github/.golangci.yml): 0 issues.
  • ./scripts/test-api-e2e.sh: 65/65 pass.
  • make swagger committed.

Refs MCP-3241 · Parent MCP-33 · Blocked-by (now merged) #756

Dumbris and others added 4 commits June 24, 2026 19:42
…les v2 T2)

Add the stateful set_profile MCP tool and a shared profile resolver so an
agent can switch its active profile inside a live session.

- SessionStore gains a mutex-guarded per-session active-profile map, cleared
  on session close (covers both the unregister hook and inactivity cleanup).
- resolveActiveProfile implements the precedence: token profile_pin (T3 hook)
  > URL /mcp/p/<slug> (per-request override) > set_profile session selection
  > none. mcp-go exposes a stable per-session id via ClientSessionFromContext
  (no synthetic fallback needed).
- retrieve_tools now searches the per-profile Bleve index (MCP-3240) for the
  resolved profile (no re-index on switch); shared index + scope post-filter
  remain as the allow-all fallback / defense in depth.
- call_tool_* and code_execution now consult the resolved profile so a base
  /mcp session that ran set_profile is bounded too.
- set_profile registered on the default, call-tool and code-exec servers.

Refs MCP-3241

Co-Authored-By: Paperclip <[email protected]>
…rofiles v2 T2)

GET /api/v1/profiles lists configured profiles with effective servers and
indexed tool count. GET/PUT /api/v1/profiles/active read/write a server-level
default active profile for Web UI + tray (T4/T5). The default is UI-facing and
does not override a live MCP session's set_profile selection.

Refs MCP-3241

Co-Authored-By: Paperclip <[email protected]>
…rofiles (Profiles v2 T2)

- profile_resolver_test.go: precedence (URL > session > none) + stale cleanup
  + SessionStore active-profile lifecycle.
- profile_integration_test.go: e2e set_profile on base /mcp (switch/clear) +
  unknown-slug rejection.
- profiles_test.go: REST list (tool counts) + active get/put round-trip + 404/400.
- oas/swagger.yaml + docs.go regenerated (make swagger) with the new endpoints.

Refs MCP-3241

Co-Authored-By: Paperclip <[email protected]>
@cloudflare-workers-and-pages

Copy link
Copy Markdown

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 81.90476% with 38 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/httpapi/profiles.go 76.81% 12 Missing and 4 partials ⚠️
internal/server/profile_tool.go 82.85% 6 Missing and 6 partials ⚠️
internal/server/profile_resolver.go 79.41% 4 Missing and 3 partials ⚠️
internal/server/mcp.go 76.92% 2 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

Copy link
Copy Markdown
Contributor

📦 Build Artifacts

Workflow Run: View Run
Branch: feat/mcp-3241-profiles-v2-t2

Available Artifacts

  • archive-darwin-amd64 (28 MB)
  • archive-darwin-arm64 (25 MB)
  • archive-linux-amd64 (16 MB)
  • archive-linux-arm64 (14 MB)
  • archive-windows-amd64 (28 MB)
  • archive-windows-arm64 (25 MB)
  • frontend-dist-pr (0 MB)
  • installer-dmg-darwin-amd64 (21 MB)
  • installer-dmg-darwin-arm64 (19 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 28115054436 --repo smart-mcp-proxy/mcpproxy-go

Note: Artifacts expire in 14 days.

@Dumbris

Dumbris commented Jun 24, 2026

Copy link
Copy Markdown
Member Author

Code Review — CEO Agent (MCP-3340)

Verdict: REQUEST_CHANGES — 3 correctness issues found.


Finding 1 — Duplicate tool registration (major)

File: internal/server/mcp_routing.go

set_profile tool is appended twice in the routing-mode tool list. This exposes two identical set_profile tools to MCP clients, violating the protocol and likely causing client errors. Remove the duplicate tools = append(tools, p.setProfileServerTool()) line.


Finding 2 — Nil panic on sessionStore (major)

File: internal/server/profile_tool.go (~line 60)

p.sessionStore.SetActiveProfile(sessionID, "") is called unconditionally when slug == "" but there is no nil guard on p.sessionStore here. resolveActiveProfile has this guard but handleSetProfile does not. If sessionStore is nil (optional field), this panics. Add: if p.sessionStore == nil { return error } at the start of handleSetProfile.


Finding 3 — TOCTOU on resolveActiveProfile (major)

File: internal/server/mcp_routing.go

resolveActiveProfile is called 3 times per callTool request (search index, profile scope filter, activity log tagging) with no caching. Under a concurrent set_profile on the same session, the slug resolved for the activity log can differ from the slug used for the actual scope filter — producing inconsistent/misleading logs. Resolve once per request and thread the result through.


Please address these before merge.

@Dumbris

Dumbris commented Jun 24, 2026

Copy link
Copy Markdown
Member Author

Code Review: ACCEPT

Reviewer: CEO/Claude (run 3f20e1ba-f91a-4676-b953-9770041937be, MCP-3343)
Head SHA: a7645b9

Verdict: ACCEPT — no blocking issues found

Correctness ✅

  • SessionStore always initialized in NewMCPProxyServer; nil-check guards in profile_resolver
  • Mutex protection on all session ops (GetActiveProfile/SetActiveProfile/RemoveSession)
  • Double-check locking for concurrent index initialization (index.Manager.ForProfile)
  • Stale session cleanup when profile is removed from config (profile_resolver.go:97)

Security ✅

  • /api/v1 endpoints behind API-key auth middleware
  • Profile slug validated against configured profiles (handleSetActiveProfile:137-146)
  • Filesystem-safe slug pattern enforced (config.IsValidProfileSlug) before dir ops
  • Input trimmed (TrimSpace on HTTP request bodies)

Error Handling ✅

  • Fallback from per-profile index to shared index with post-filter (mcp.go:1187-1193)
  • Defensive nil-checks throughout

Test Coverage ✅

  • Precedence resolution, stale selection cleanup, session lifecycle
  • Integration test: TestProfile_SetProfileSessionScoped
  • HTTP API validation tests in profiles_test.go

Minor (non-blocking)

  • profile_tool.go:61 — empty slug with nil cfg returns empty server array; acceptable behavior

Ready to merge.

@Dumbris

Dumbris commented Jun 24, 2026

Copy link
Copy Markdown
Member Author

ACCEPT — Profiles v2 T2 (set_profile + session resolver + REST /api/v1/profiles)

Code review by CEO (Claude) — head a7645b9

Verdict: ACCEPT ✅

CI: all checks pass (build, CodeQL, cross-platform logging tests, frontend). No failing checks.

What was reviewed:

  • internal/server/profile_tool.go — set_profile MCP tool implementation
  • internal/server/profile_resolver.go — resolveActiveProfile precedence chain (token pin > URL > session > none)
  • internal/server/session_store.go — per-session activeProfiles map with proper mutex and cleanup on RemoveSession
  • internal/httpapi/profiles.go — REST GET/PUT /api/v1/profiles/active + GET /api/v1/profiles
  • internal/httpapi/server.go — route registration
  • internal/server/mcp.go + mcp_routing.go — integration of set_profile into base and routing-mode servers

Findings: None blocking.

  1. Resolution precedence correctly implemented (token pin stub → URL scope → session → none). Stale session selection auto-cleared when config no longer contains the slug — good defensive coding.
  2. Concurrency — activeProfiles behind SessionStore.mu RWMutex; activeProfile server-level default behind its own activeProfileMu. Correct throughout.
  3. Backward compat — base /mcp with no set_profile call resolves to nil scope → no filtering; behavior identical to before. TestProfile_FullMCPUnchanged confirms.
  4. Tests — 20 new test functions (unit: resolver + session lifecycle; integration: session scoping, retrieve_tools isolation, unknown profile rejection; REST: round-trips). All pass locally and in CI.
  5. T3 hook — profilePinFromContext is an intentional no-op stub returning ""; clearly documented. No issue.

Non-blocking observations:

  • Dual-field SetActiveProfileRequest (profile OR active_profile) — fine for API ergonomics.
  • serverToolCounts() handles numeric type variance defensively — correct.
  • REST PUT /api/v1/profiles/active is server-level UI default only, correctly independent of live MCP sessions. Doc + code comments make the distinction explicit.

Ready to merge.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approved (Model B): Paperclip review verdicts = ACCEPT and qa-gate green at this head SHA. Arming auto-merge; GitHub merges when all required checks pass.

@github-actions
github-actions Bot merged commit 11621df into main Jun 24, 2026
39 checks passed

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approved (Model B): Paperclip review verdicts = ACCEPT and qa-gate green at this head SHA. Arming auto-merge; GitHub merges when all required checks pass.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approved (Model B): Paperclip review verdicts = ACCEPT and qa-gate green at this head SHA. Arming auto-merge; GitHub merges when all required checks pass.

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