search-problems
Filter and retrieve LeetCode problems by tags, difficulty, or limit results. Use this tool to find specific coding challenges for practice or preparation.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| difficulty | No | Difficulty level | |
| limit | No | Maximum number of problems to return | |
| skip | No | Number of problems to skip | |
| tags | No | Tags to filter by, separated by '+' (e.g., 'array+dynamic-programming') |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"difficulty": {
"description": "Difficulty level",
"enum": [
"EASY",
"MEDIUM",
"HARD"
],
"type": "string"
},
"limit": {
"default": 20,
"description": "Maximum number of problems to return",
"maximum": 100,
"minimum": 1,
"type": "number"
},
"skip": {
"default": 0,
"description": "Number of problems to skip",
"type": "number"
},
"tags": {
"description": "Tags to filter by, separated by '+' (e.g., 'array+dynamic-programming')",
"type": "string"
}
},
"type": "object"
}
Implementation Reference
- src/tools/problem-tools.ts:63-79 (handler)The handler function for the "search-problems" MCP tool. It receives input parameters, calls LeetCodeService.searchProblems, formats the result as JSON text content, and handles errors.async ({ tags, difficulty, limit, skip }) => { try { const data = await leetcodeService.searchProblems(tags, difficulty, limit, skip); return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] }; } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : String(error); return { content: [{ type: "text", text: `Error: ${errorMessage}` }], isError: true }; } }
- src/tools/problem-tools.ts:57-62 (schema)Zod input schema defining parameters for the search-problems tool: optional tags, difficulty, limit, and skip.{ tags: z.string().optional().describe("Tags to filter by, separated by '+' (e.g., 'array+dynamic-programming')"), difficulty: z.enum(["EASY", "MEDIUM", "HARD"]).optional().describe("Difficulty level"), limit: z.number().min(1).max(100).optional().default(20).describe("Maximum number of problems to return"), skip: z.number().optional().default(0).describe("Number of problems to skip") },
- src/tools/problem-tools.ts:54-80 (registration)Registration of the "search-problems" tool using McpServer.tool(), including schema and handler.// Search problems server.tool( "search-problems", { tags: z.string().optional().describe("Tags to filter by, separated by '+' (e.g., 'array+dynamic-programming')"), difficulty: z.enum(["EASY", "MEDIUM", "HARD"]).optional().describe("Difficulty level"), limit: z.number().min(1).max(100).optional().default(20).describe("Maximum number of problems to return"), skip: z.number().optional().default(0).describe("Number of problems to skip") }, async ({ tags, difficulty, limit, skip }) => { try { const data = await leetcodeService.searchProblems(tags, difficulty, limit, skip); return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] }; } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : String(error); return { content: [{ type: "text", text: `Error: ${errorMessage}` }], isError: true }; } } );
- Helper method in LeetCodeService that executes the GraphQL search query for problems based on tags, difficulty, limit, and skip.async searchProblems(tags?: string, difficulty?: string, limit: number = 20, skip: number = 0) { return this.executeQuery(searchProblemsQuery, { categorySlug: "", limit, skip, filters: { tags: tags ? tags.split("+") : [], difficulty: difficulty || null } }); }
- src/graphql/queries.ts:146-175 (helper)GraphQL query definition used by LeetCodeService.searchProblems to fetch problem list from LeetCode API.export const searchProblemsQuery = ` query problemsetQuestionList($categorySlug: String, $limit: Int, $skip: Int, $filters: QuestionListFilterInput) { problemsetQuestionList: questionList( categorySlug: $categorySlug limit: $limit skip: $skip filters: $filters ) { total: totalNum questions: data { acRate difficulty freqBar frontendQuestionId: questionFrontendId isFavor paidOnly: isPaidOnly status title titleSlug topicTags { name id slug } hasSolution hasVideoSolution } } } `;