(server: McpServer, leetcodeService: LeetCodeService)
| 3 | import { z } from "zod"; |
| 4 | |
| 5 | export function registerProblemTools(server: McpServer, leetcodeService: LeetCodeService) { |
| 6 | // Get daily challenge |
| 7 | server.tool( |
| 8 | "get-daily-challenge", |
| 9 | {}, |
| 10 | async () => { |
| 11 | try { |
| 12 | const data = await leetcodeService.fetchDailyChallenge(); |
| 13 | return { |
| 14 | content: [{ |
| 15 | type: "text", |
| 16 | text: JSON.stringify(data, null, 2) |
| 17 | }] |
| 18 | }; |
| 19 | } catch (error: unknown) { |
| 20 | const errorMessage = error instanceof Error ? error.message : String(error); |
| 21 | return { |
| 22 | content: [{ type: "text", text: `Error: ${errorMessage}` }], |
| 23 | isError: true |
| 24 | }; |
| 25 | } |
| 26 | } |
| 27 | ); |
| 28 | |
| 29 | // Get problem details |
| 30 | server.tool( |
| 31 | "get-problem", |
| 32 | { |
| 33 | titleSlug: z.string().describe("The URL slug of the problem (e.g., 'two-sum')") |
| 34 | }, |
| 35 | async ({ titleSlug }) => { |
| 36 | try { |
| 37 | const data = await leetcodeService.fetchProblem(titleSlug); |
| 38 | return { |
| 39 | content: [{ |
| 40 | type: "text", |
| 41 | text: JSON.stringify(data, null, 2) |
| 42 | }] |
| 43 | }; |
| 44 | } catch (error: unknown) { |
| 45 | const errorMessage = error instanceof Error ? error.message : String(error); |
| 46 | return { |
| 47 | content: [{ type: "text", text: `Error: ${errorMessage}` }], |
| 48 | isError: true |
| 49 | }; |
| 50 | } |
| 51 | } |
| 52 | ); |
| 53 | |
| 54 | // Search problems |
| 55 | server.tool( |
| 56 | "search-problems", |
| 57 | { |
| 58 | tags: z.string().optional().describe("Tags to filter by, separated by '+' (e.g., 'array+dynamic-programming')"), |
| 59 | difficulty: z.enum(["EASY", "MEDIUM", "HARD"]).optional().describe("Difficulty level"), |
| 60 | limit: z.number().min(1).max(100).optional().default(20).describe("Maximum number of problems to return"), |
| 61 | skip: z.number().optional().default(0).describe("Number of problems to skip") |
| 62 | }, |
no test coverage detected