Skip to content

Conversation

@devroopsaha744
Copy link

Summary

This PR adds a modular Model Context Protocol (MCP) server that exposes LeetCode functionality as MCP tools and documents how to run and inspect it with the MCP Inspector and Claude Desktop. The MCP implementation is split into three modules: users, problems, and discussions. It includes a shared server bootstrap, an Inspector launcher, and README updates with setup and usage instructions.

What changed

Add modular MCP entrypoint and bootstrap

  • index.ts - entrypoint that composes modules and supports running a single module or the full suite via CLI arg or environment variable
  • serverUtils.ts - shared helpers for server lifecycle and GraphQL execution; fix to avoid double-starting stdio transport
  • leetCodeService.ts - LeetCode GraphQL query helpers used by modules

Add tool modules

  • userTools.ts - user related tools (profile, badges, submissions, calendar, etc.)
  • problemTools.ts - problem related tools (daily, select, list, filters)
  • discussionTools.ts - discussion related tools (trending, topic, comments)

Dev and inspect helpers

  • runInspector.ts - helper to launch the official MCP Inspector against this server
  • package.json - add script mcp:inspect

Documentation

  • README.md - add "MCP server integration" section with build, Inspector, and Claude Desktop configuration examples using generic paths

Naming and validation

Tool identifiers updated to conform to MCP name pattern (only alphanumeric, underscore, hyphen). Example: leetcode_user_data

Files added or modified (high level)

Added

  • index.ts
  • serverUtils.ts
  • leetCodeService.ts
  • userTools.ts
  • problemTools.ts
  • discussionTools.ts
  • runInspector.ts

Modified

  • package.json (scripts)
  • README.md (MCP integration docs)

Rationale

  • Provide interactive, LLM-friendly access to LeetCode data without requiring the REST API as an intermediary.
  • Make tools modular so developers can run only the domain they need during development or deployment.
  • Ensure tool names conform to MCP client validation so they appear in Inspector and Claude Desktop.

How to test

Install and build

npm install
npm run build

Configure Claude Desktop

  1. Open %AppData%\Claude\claude_desktop_config.json.
  2. Add a server entry pointing at the built file. Example:
   {
     "mcpServers": {
       "leetcode-suite": {
         "command": "node",
         "args": ["C:\\path\\to\\alfa-leetcode-api\\dist\\mcp\\index.js"]
       }
     }
   }
  1. Restart Claude Desktop and verify tools appear under Search and tools

Demo

mcp-example-2 mcp-example-4

Notes and compatibility

  • The MCP server queries https://leetcode.com/graphql directly. It does not call the existing HTTP REST API in this repo.
  • HTTP REST endpoints remain unchanged.
  • Tool names were changed to meet MCP naming rules. Clients must use the updated identifiers.
  • The server is subject to the same rate limits as direct GraphQL calls. Use Inspector to validate tool schemas before integrating into Claude Desktop.

Checklist

  • ✅ Typescript build passes: npm run build
  • ✅ Tool identifiers conform to MCP name pattern
  • ✅ README updated with MCP integration instructions
  • ⚠️ Manual Inspector test recommended before Claude Desktop integration

@alfaarghya
Copy link
Owner

hii @devroopsaha744

I appreciate your efforts and contributions, but I was unable to find the previous discussion for this feature!

Could you provide the issue number or the discussion number?

@devroopsaha744
Copy link
Author

devroopsaha744 commented Oct 25, 2025

hi @alfaarghya

Thanks for the feedback! I wasn’t aware that prior discussion or an issue was required before opening a feature PR. The goal of this PR is to add an MCP (Model Context Protocol) server layer so that the API can be accessed directly by AI agents and LLMs through Claude Desktop or similar tools.

Shall I open a discussion for this feature and then link it back here (if approved)?

@alfaarghya
Copy link
Owner

Yes, in every open-source project, you first need to ask the maintainer or the community about the feature you want to add by posting an issue or opening up a discussion.

So, please open up some discussion or feature(issue), so that others can give their opinions and also let's discuss if we need this feature or not!

Meanwhile, I will be going through your changes and reviewing your code.

@devroopsaha744
Copy link
Author

hi @alfaarghya

I’ve opened a detailed discussion outlining this feature proposal here: #73

It covers the motivation, technical breakdown, and rationale behind adding the MCP server for AI Agent and LLM integration.
We can continue the conversation there to evaluate alignment before reopening or refining the PR.

Copy link
Owner

@alfaarghya alfaarghya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hii @devroopsaha744

Your implementation looks solid! But we need some changes before I merge it!

  • add proper comments for your functions
  • move type or interface to other directory or file.
  • Provide instructions for other OS like Linux and MAC OS

mcp/index.ts Outdated
Comment on lines 6 to 11
type Mode = 'all' | 'users' | 'problems' | 'discussions';

type ModuleConfig = {
modules: ToolModule[];
name: string;
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw you are putting types in the files themselves. But I would suggest trying to keep them in a separate file or directory(like mcp/types.ts).

README.md Outdated
Comment on lines 52 to 64
1. Open `%AppData%/Claude/claude_desktop_config.json`.
2. Add a server entry pointing at the built file. Example:

```json
{
"mcpServers": {
"leetcode-suite": {
"command": "node",
"args": ["C:\\path\\to\\alfa-leetcode-api\\dist\\mcp\\index.js"]
}
}
}
```
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how we can configure the MCP server for Windows. What about Linux and MAC OS?

Need instructions for those!

Comment on lines 47 to 52
type SubmissionArgs = { username: string; limit?: number };
type CalendarArgs = { username: string; year: number };
type ProblemArgs = { limit?: number; skip?: number; tags?: string; difficulty?: string };
type DiscussCommentsArgs = { topicId: number; orderBy?: string; pageNo?: number; numPerPage?: number };

type Variables = Record<string, unknown>;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep types in a separate file(mcp/types.ts)

import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';

type GraphQLParams = Record<string, unknown>;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep types in a separate file(mcp/types.ts)

type GraphQLParams = Record<string, unknown>;

const GRAPHQL_ENDPOINT = 'https://leetcode.com/graphql';
export const SERVER_VERSION = '2.0.1';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current server version is 2.0.2.

But why do we need this?

Comment on lines 75 to 77
export type ToolResponse = { content: { type: 'text'; text: string }[] };

export type ToolExecutor = () => Promise<unknown>;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep types in a separate file(mcp/types.ts)

@devroopsaha744
Copy link
Author

devroopsaha744 commented Oct 31, 2025

Hi @alfaarghya

first of all sorry the delay.

I have implemented the changes as per your request:

  1. moved the types and interfaces onto one file
  2. corrected the server version, it was a typo actually, it is was meant to be the version of the MCP server not the API server
  3. Regarding the instructions, these are same for all the operating systems as long as that particular MCP client (claude desktop, cursor etc) is installed in that operating system and made the setup instructions more clear and robust.

can you please review them

@alfaarghya
Copy link
Owner

Hi @devroopsaha744, I'll review your changes, but meanwhile, please resolve the merge conflicts on package-lock.json.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolve these merge conflicts

Copy link
Owner

@alfaarghya alfaarghya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hii @devroopsaha744, your code looks solid, and I will approve the PR. But first, please resolve the conflict!

@devroopsaha744
Copy link
Author

Hi @alfaarghya , I have resolved the conflicts. let me know if you need anything else!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants