Skip to content

fix(web): disambiguate MCP language model selection for ask_codebase#1414

Open
Harsh23Kashyap wants to merge 4 commits into
sourcebot-dev:mainfrom
Harsh23Kashyap:Harsh23Kashyap/fix-mcp-language-model-matching-1137
Open

fix(web): disambiguate MCP language model selection for ask_codebase#1414
Harsh23Kashyap wants to merge 4 commits into
sourcebot-dev:mainfrom
Harsh23Kashyap:Harsh23Kashyap/fix-mcp-language-model-matching-1137

Conversation

@Harsh23Kashyap

@Harsh23Kashyap Harsh23Kashyap commented Jul 2, 2026

Copy link
Copy Markdown

Fixes #1137

Summary

  • match askCodebase requests by provider and model before considering displayName
  • require displayName only when multiple configured models share the same provider and model
  • add focused unit coverage for the selection and disambiguation cases

Test plan

  • yarn workspace @sourcebot/web test --run "src/ee/features/mcp/askCodebase.test.ts"
  • yarn workspace @sourcebot/web lint "src/ee/features/mcp/askCodebase.ts" "src/ee/features/mcp/askCodebase.test.ts"
  • ReadLints clean for the touched files
  • local Yarn commands could not run here because the repo is missing the Yarn install state (node_modules state file)

Summary by CodeRabbit

  • Bug Fixes
    • Corrected language model resolution to match configured models by provider and model, using displayName only when multiple configured entries would otherwise be ambiguous.
    • Improved validation and error responses for missing matches, missing required displayName, and invalid displayName values.
  • Tests
    • Added tests covering correct selection, duplicate-model disambiguation, and BAD_REQUEST invalid request scenarios.
  • Documentation
    • Updated the changelog under Unreleased with the language model selection fix.

@coderabbitai

coderabbitai Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: cadcd90d-ce6b-4074-b711-69cc6686b9ee

📥 Commits

Reviewing files that changed from the base of the PR and between 72b615d and e6d4b01.

📒 Files selected for processing (1)
  • CHANGELOG.md
✅ Files skipped from review due to trivial changes (1)
  • CHANGELOG.md

Walkthrough

This PR fixes MCP ask_codebase language model selection by matching configured models on provider and model, using displayName only to disambiguate duplicate configurations, and adding tests plus a changelog note.

Changes

Language model selection fix

Layer / File(s) Summary
selectConfiguredLanguageModel helper and type
packages/web/src/ee/features/mcp/askCodebase.ts
Adds a ConfiguredLanguageModel type and the selectConfiguredLanguageModel function matching by provider+model, disambiguating via displayName, and returning ServiceError for no match, wrong displayName, or unresolved multiple matches; removes the unused getLanguageModelKey import.
Wire selection helper into askCodebase
packages/web/src/ee/features/mcp/askCodebase.ts
Replaces the previous inline getLanguageModelKey-based matching with a call to selectConfiguredLanguageModel, propagating its ServiceError or returning an internal error if no config is unexpectedly resolved.
Selection helper tests and changelog
packages/web/src/ee/features/mcp/askCodebase.test.ts, CHANGELOG.md
Adds a Vitest suite covering successful selection, displayName disambiguation, and BAD_REQUEST/INVALID_REQUEST_BODY error cases, plus a changelog entry describing the fix.

Estimated code review effort: 2 (Simple) | ~15 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant askCodebase
  participant selectConfiguredLanguageModel

  Client->>askCodebase: request with languageModel (provider, model)
  askCodebase->>selectConfiguredLanguageModel: configuredModels, requestedLanguageModel
  alt no match
    selectConfiguredLanguageModel-->>askCodebase: ServiceError (not configured)
  else displayName mismatch
    selectConfiguredLanguageModel-->>askCodebase: ServiceError (wrong displayName)
  else multiple matches, no displayName
    selectConfiguredLanguageModel-->>askCodebase: ServiceError (ambiguous, need displayName)
  else single match
    selectConfiguredLanguageModel-->>askCodebase: languageModelConfig
  end
  askCodebase-->>Client: ServiceError or selected config
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: fixing MCP ask_codebase model selection and disambiguation.
Linked Issues check ✅ Passed The changes match #1137 by selecting models via provider/model first, using displayName only to disambiguate, and adding coverage.
Out of Scope Changes check ✅ Passed The PR stays focused on ask_codebase selection logic, tests, and changelog notes with no obvious unrelated code changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai 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.

🧹 Nitpick comments (1)
packages/web/src/ee/features/mcp/askCodebase.test.ts (1)

19-85: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Missing test for the "not configured" (zero candidates) branch.

The suite covers omitted-displayName match, displayName disambiguation, multiple-match error, and displayName-mismatch error, but doesn't test the case where no configured model matches provider+model at all (candidateModels.length === 0 branch in selectConfiguredLanguageModel, packages/web/src/ee/features/mcp/askCodebase.ts Lines 56-64).

✅ Suggested additional test
+    it("returns an error when no configured model matches provider and model", () => {
+        const configuredModel = createConfiguredModel();
+
+        const result = selectConfiguredLanguageModel(
+            [configuredModel],
+            { provider: 'openai', model: 'gpt-5' }
+        );
+
+        expect(result).toEqual({
+            error: {
+                statusCode: StatusCodes.BAD_REQUEST,
+                errorCode: ErrorCode.INVALID_REQUEST_BODY,
+                message: "Language model 'openai/gpt-5' is not configured.",
+            },
+        });
+    });
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/web/src/ee/features/mcp/askCodebase.test.ts` around lines 19 - 85,
Add a test in selectConfiguredLanguageModel’s describe block to cover the
zero-candidate branch where no configured model matches the requested
provider/model. Use createConfiguredModel with a non-matching provider or model,
call selectConfiguredLanguageModel with that request, and assert it returns the
expected not-configured error shape rather than a languageModelConfig. This
should sit alongside the existing cases for disambiguation and displayName
mismatch so the candidateModels.length === 0 path is covered.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/web/src/ee/features/mcp/askCodebase.test.ts`:
- Around line 19-85: Add a test in selectConfiguredLanguageModel’s describe
block to cover the zero-candidate branch where no configured model matches the
requested provider/model. Use createConfiguredModel with a non-matching provider
or model, call selectConfiguredLanguageModel with that request, and assert it
returns the expected not-configured error shape rather than a
languageModelConfig. This should sit alongside the existing cases for
disambiguation and displayName mismatch so the candidateModels.length === 0 path
is covered.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f43ad8e7-f916-49f8-a763-7d2b548aa79c

📥 Commits

Reviewing files that changed from the base of the PR and between fd6720f and 72b615d.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • packages/web/src/ee/features/mcp/askCodebase.test.ts
  • packages/web/src/ee/features/mcp/askCodebase.ts

@Harsh23Kashyap

Copy link
Copy Markdown
Author

Can someone take a look when you get a chance?

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.

MCP ask_codebase rejects explicit languageModel: getLanguageModelKey includes displayName which the MCP schema doesn't expose

1 participant