Skip to content

Commit 54ed3cb

Browse files
author
a.dmitriev
committed
fix(mcp): detect ambiguous model selection and return actionable 400
When displayName is absent and multiple configs share the same provider+model, return a 400 that lists candidate displayNames instead of silently picking the first match. Matching logic: - filter by provider+model to collect candidates - if displayName provided: exact match on displayName within candidates - if displayName absent and exactly one candidate: use it - if displayName absent and multiple candidates: 400 with disambiguation hint
1 parent 502b4ac commit 54ed3cb

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

packages/web/src/features/mcp/askCodebase.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,22 @@ export const askCodebase = (params: AskCodebaseParams): Promise<AskCodebaseResul
5757

5858
let languageModelConfig = configuredModels[0];
5959
if (requestedLanguageModel) {
60-
const matchingModel = configuredModels.find(
60+
const candidates = configuredModels.filter(
6161
(m) => m.provider === requestedLanguageModel.provider &&
62-
m.model === requestedLanguageModel.model &&
63-
(requestedLanguageModel.displayName === undefined ||
64-
m.displayName === requestedLanguageModel.displayName)
62+
m.model === requestedLanguageModel.model
6563
);
64+
const matchingModel = requestedLanguageModel.displayName
65+
? candidates.find((m) => m.displayName === requestedLanguageModel.displayName)
66+
: candidates.length === 1 ? candidates[0] : undefined;
6667
if (!matchingModel) {
68+
const ambiguous = !requestedLanguageModel.displayName && candidates.length > 1;
69+
const hint = ambiguous
70+
? ` Multiple configurations found for this model. Provide a displayName to disambiguate: ${candidates.map((m) => m.displayName).join(', ')}.`
71+
: '';
6772
return {
6873
statusCode: StatusCodes.BAD_REQUEST,
6974
errorCode: ErrorCode.INVALID_REQUEST_BODY,
70-
message: `Language model '${requestedLanguageModel.provider}/${requestedLanguageModel.model}' is not configured.`,
75+
message: `Language model '${requestedLanguageModel.provider}/${requestedLanguageModel.model}' is not configured.${hint}`,
7176
} satisfies ServiceError;
7277
}
7378
languageModelConfig = matchingModel;

0 commit comments

Comments
 (0)