Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion src/graphql/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,57 @@ export const userContestRankingQuery = `
}
}
}
`;
`;

export const randomProblemQuery = `
query randomQuestion($categorySlug: String, $filters: QuestionListFilterInput) {
randomQuestion(categorySlug: $categorySlug, filters: $filters) {
questionId
questionFrontendId
boundTopicId
title
titleSlug
content
translatedTitle
difficulty
likes
dislikes
isLiked
similarQuestions
exampleTestcases
contributors {
username
profileUrl
avatarUrl
}
topicTags {
name
slug
}
companyTagStats
codeSnippets {
lang
langSlug
code
}
stats
hints
solution {
id
canSeeDetail
paidOnly
hasVideoSolution
}
status
sampleTestCase
metaData
judgerAvailable
judgeType
mysqlSchemas
enableRunCode
enableTestMode
enableDebugger
envInfo
}
}
`;
18 changes: 16 additions & 2 deletions src/services/leetcode-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
problemDetailsQuery,
searchProblemsQuery,
contestDetailsQuery,
userContestRankingQuery
userContestRankingQuery,
randomProblemQuery
} from "../graphql/queries.js";

export class LeetCodeService {
Expand Down Expand Up @@ -80,6 +81,19 @@ export class LeetCodeService {
return this.executeQuery(problemDetailsQuery, { titleSlug });
}

/**
* Fetch a random problem based on the given criteria
*/
async fetchRandomProblem(tags?: string, difficulty?: string) {
return this.executeQuery(randomProblemQuery, {
categorySlug: "",
filters: {
tags: tags ? tags.split("+") : [],
difficulty: difficulty || null,
}
});
}

/**
* Search for problems matching criteria
*/
Expand Down Expand Up @@ -108,4 +122,4 @@ export class LeetCodeService {
async fetchUserContestRanking(username: string) {
return this.executeQuery(userContestRankingQuery, { username });
}
}
}
28 changes: 27 additions & 1 deletion src/tools/problem-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,30 @@ export function registerProblemTools(server: McpServer, leetcodeService: LeetCod
}
}
);
}

// Random Problem
server.tool(
"random-problem",
{
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")
},
async ({tags, difficulty}) => {
try {
const data = await leetcodeService.fetchRandomProblem(tags, difficulty);
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
};
}
}
);
}