Skip to content

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

Open
Harsh23Kashyap wants to merge 3 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 3 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
    • Improved language model selection so the app now matches configured models by provider and model, and only asks for a display name when needed to tell similar entries apart.
    • Added clearer error handling when no matching model is found or when the chosen name doesn’t exist.
  • Tests
    • Added coverage for model selection, duplicate-model disambiguation, and invalid request cases.
  • Documentation
    • Updated the changelog with the latest fix.

@coderabbitai

coderabbitai Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

This PR fixes MCP ask_codebase language model selection by adding selectConfiguredLanguageModel, which matches configured models on provider+model and only requires displayName for disambiguation when multiple configured entries share that pair, replacing the previous key-based match that incorrectly required displayName always. Tests and a changelog entry are added.

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 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

Suggested reviewers: jsourcebot

🚥 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 and concisely describes the main change: fixing ask_codebase language model selection disambiguation.
Linked Issues check ✅ Passed The code matches provider/model first, uses displayName only to break ties, preserves default selection, and adds tests for the reported cases.
Out of Scope Changes check ✅ Passed The diff stays focused on the model-selection fix, related tests, and a changelog note with no unrelated feature work.
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

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