fix: change safesearch parameter type from string to number - #48
Conversation
The safesearch parameter was defined as type "string" with enum ["0", "1", "2"] in the JSON schema. LLMs naturally send numeric values (0, 1, 2) which caused MCP SDK schema validation to fail with: "params.safesearch is not of a type(s) string params.safesearch is not one of enum values: 0,1,2" Changed to type "number" with enum [0, 1, 2] to match what LLMs send and what the SearXNG API expects. Co-authored-by: ihor-sokoliuk <17313213+ihor-sokoliuk@users.noreply.github.com>
|
@copilot, what parameter type does the SearXNG server API endpoint expect to receive? Is it a number or a string? |
The SearXNG Our code handles the conversion correctly at |
There was a problem hiding this comment.
Pull request overview
This PR fixes MCP tool argument schema validation failures by changing the safesearch parameter from a string enum ("0" | "1" | "2") to a numeric enum (0 | 1 | 2) throughout the SDK surface and implementation, aligning with typical LLM outputs and the SearXNG API.
Changes:
- Update
searxng_web_searchinput schema and TS types to treatsafesearchas anumberenum. - Update
performWebSearchto accept numericsafesearch, validate it against[0,1,2], and serialize it for URL query params. - Update help text and unit test to use numeric
safesearch.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/types.ts |
Switches schema/type for safesearch to numeric enum in the tool input schema and TS arg typing. |
src/search.ts |
Updates performWebSearch signature and URL param construction for numeric safesearch. |
src/resources.ts |
Updates usage/help text to document numeric safesearch values. |
__tests__/unit/search.test.ts |
Adjusts test callsite to pass numeric safesearch and still assert correct URL serialization. |
Comments suppressed due to low confidence (1)
src/types.ts:24
isSearXNGWebSearchArgsnow advertisessafesearch?: number, but the guard only validatesqueryand will return true even ifsafesearchis provided with a non-number type. This makes the type predicate unsound and can lead to silently ignoring user-providedsafesearchvalues at runtime. Consider extending this guard to validate (and ideally range-check) optional fields likepageno,time_range,language, andsafesearchwhen they are present.
export function isSearXNGWebSearchArgs(args: unknown): args is {
query: string;
pageno?: number;
time_range?: string;
language?: string;
safesearch?: number;
} {
return (
typeof args === "object" &&
args !== null &&
"query" in args &&
typeof (args as { query: string }).query === "string"
);
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| pageno: number = 1, | ||
| time_range?: string, | ||
| language: string = "all", | ||
| safesearch?: string | ||
| safesearch?: number | ||
| ) { |
There was a problem hiding this comment.
After changing safesearch to a number, the existing logging code inside this function uses a truthiness check (safesearch ? ...) when building searchParams, which will treat the valid value 0 as “not set” and omit it from logs. Use an explicit safesearch !== undefined check so 0 is logged correctly.
MCP SDK validates tool arguments against the JSON schema before the handler runs.
safesearchwas defined astype: "string"withenum: ["0", "1", "2"], but LLMs send numeric values, causing schema validation to fail:Changed
safesearchtotype: "number"withenum: [0, 1, 2]across the stack:src/types.ts— Schema typestring→number, enum["0","1","2"]→[0,1,2], TypeScript type updatedsrc/search.ts— Function signaturesafesearch?: string→number, validation against numeric enum,.toString()for URL param serializationsrc/resources.ts— Help text updated to reflect numeric values__tests__/unit/search.test.ts— Test passes numeric1instead of string'1'This aligns with the SearXNG API (which accepts numeric safesearch) and the README (which already documented it as
number).Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.