Skip to content

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

Open
Harsh23Kashyap wants to merge 5 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 5 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
  • extract selectConfiguredLanguageModel into a server-free module so vitest can run without server-only imports

Test plan

  • yarn workspace @sourcebot/web test --run src/ee/features/mcp/askCodebase.test.ts (4 passed)
  • yarn workspace @sourcebot/web lint on touched files (0 errors)
  • yarn build:deps before test run
  • ReadLints clean for the touched files

@coderabbitai

coderabbitai Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

This PR updates MCP ask_codebase to select configured language models by provider and model, use displayName only for disambiguation, and adds tests plus a changelog note.

Changes

Language model selection fix

Layer / File(s) Summary
Selection helper
packages/web/src/ee/features/mcp/selectConfiguredLanguageModel.ts
Adds helper formatting logic and a resolver that matches configured models by provider and model, returning structured ServiceError responses for missing, ambiguous, or displayName-mismatched requests.
askCodebase wiring
packages/web/src/ee/features/mcp/askCodebase.ts
Removes getLanguageModelKey, imports selectConfiguredLanguageModel, and routes explicit language model requests through the helper with error propagation and fallback handling.
Tests and changelog
packages/web/src/ee/features/mcp/askCodebase.test.ts, CHANGELOG.md
Adds unit coverage for selection success and failure cases, and records the fix in the changelog.

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

Sequence Diagram(s)

sequenceDiagram
  participant askCodebase
  participant selectConfiguredLanguageModel

  askCodebase->>selectConfiguredLanguageModel: configuredModels, requestedLanguageModel
  alt no match
    selectConfiguredLanguageModel-->>askCodebase: ServiceError
  else displayName mismatch
    selectConfiguredLanguageModel-->>askCodebase: ServiceError
  else multiple matches without displayName
    selectConfiguredLanguageModel-->>askCodebase: ServiceError
  else single match
    selectConfiguredLanguageModel-->>askCodebase: languageModelConfig
  end
Loading

Possibly related PRs

Suggested reviewers: msukkari

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The PR matches #1137 by selecting configured models via provider/model and using displayName only to resolve ambiguity.
Out of Scope Changes check ✅ Passed The changes stay within scope, adding the selector refactor, tests, and a changelog note for the same fix.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main fix: disambiguating ask_codebase language model selection.
✨ 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?

@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/selectConfiguredLanguageModel.ts (1)

12-62: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Matching logic looks correct; consider a discriminated union for the return type.

Provider/model-first matching with displayName only for disambiguation matches the PR objective and the test cases exactly. One optional improvement: since every branch sets exactly one of languageModelConfig/error, a discriminated union return type would let TypeScript enforce that exclusivity and remove the "impossible" !selectedLanguageModel fallback branch currently needed in askCodebase.ts.

♻️ Optional refactor
-): {
-    languageModelConfig?: T;
-    error?: ServiceError;
-} => {
+): (
+    | { languageModelConfig: T; error?: never }
+    | { languageModelConfig?: never; error: ServiceError }
+) => {
🤖 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/selectConfiguredLanguageModel.ts` around
lines 12 - 62, The matching logic in selectConfiguredLanguageModel is fine, but
its return shape currently allows both languageModelConfig and error to be
optional, which forces downstream fallback handling. Refactor
selectConfiguredLanguageModel to return a discriminated union with either a
success variant carrying languageModelConfig or an error variant carrying error,
and update callers such as askCodebase.ts to branch on that discriminator
instead of using an impossible fallback check.
🤖 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/selectConfiguredLanguageModel.ts`:
- Around line 12-62: The matching logic in selectConfiguredLanguageModel is
fine, but its return shape currently allows both languageModelConfig and error
to be optional, which forces downstream fallback handling. Refactor
selectConfiguredLanguageModel to return a discriminated union with either a
success variant carrying languageModelConfig or an error variant carrying error,
and update callers such as askCodebase.ts to branch on that discriminator
instead of using an impossible fallback check.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 16ff9d9f-da1a-41db-a287-410ddbedba61

📥 Commits

Reviewing files that changed from the base of the PR and between e6d4b01 and 1e7edb5.

📒 Files selected for processing (3)
  • packages/web/src/ee/features/mcp/askCodebase.test.ts
  • packages/web/src/ee/features/mcp/askCodebase.ts
  • packages/web/src/ee/features/mcp/selectConfiguredLanguageModel.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/web/src/ee/features/mcp/askCodebase.test.ts

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