LeetCode MCP
LeetCode MCP — Learn MCP by Building One
This project is two things at once:
A gentle, from-scratch tutorial on what an MCP is and how to build one.
A working LeetCode MCP that connects to your LeetCode profile so an AI assistant (Cursor / Claude) can tell you whether you've solved a problem and recommend new problems by topic and difficulty.
Part 1 — What is an MCP? (the mental model)
MCP = Model Context Protocol. It's an open standard that lets an AI model safely use external tools and data. Think of it as a USB-C port for AI: any AI client that "speaks MCP" can plug into any MCP server and instantly gain new abilities.
Three roles:
Role | What it is | In our case |
Host | The app you chat in | Cursor / Claude Desktop |
Client | Lives inside the host, speaks MCP | Built into Cursor |
Server | Your program that exposes tools/data |
|
An MCP server can expose three kinds of things:
Tools — functions the AI can call (e.g.
recommend_questions). ← we use thisResources — read-only data the AI can load (like files).
Prompts — reusable prompt templates.
How they talk: the host launches your server as a subprocess and exchanges JSON-RPC messages over stdio (standard input/output). You don't manage that plumbing — the SDK does. You just write Python functions and decorate them.
The magic of the SDK: a function's name, docstring, and type hints are automatically turned into a schema the AI reads to know when/how to call it. That's why our functions have descriptive names and detailed docstrings.
@mcp.tool()
async def recommend_questions(topic: str, difficulty: str, count: int = 5) -> dict:
"""Recommend LeetCode problems by topic and difficulty..."""
...That decorator is 90% of "creating an MCP." Everything else is normal code.
Part 2 — What THIS MCP does
It exposes three tools:
Tool | What it does |
| Your solved counts (easy/medium/hard) + global ranking. |
| "Have I solved Two Sum?" → solved / attempted / never tried. |
| Find new problems by topic + difficulty, skipping solved ones. |
Where the data comes from
LeetCode has no official public API, but its website runs on a GraphQL
endpoint at https://leetcode.com/graphql. We query it the same way the site
does (see leetcode_client.py).
Public data (problem lists, difficulty, topics) needs no login.
Your private "solved" status requires your browser session cookies so LeetCode knows it's you. With those, every problem carries a
status:"ac"= solved,"notac"= attempted,null= never tried.
Part 3 — Setup (5 minutes)
1. Install dependencies
cd /Users/akshayapratapsingh/Desktop/Leetcode-MCP
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt2. Configure your identity + cookies
cp .env.example .envThen edit .env:
LEETCODE_USERNAME→ your username fromleetcode.com/u/<username>/.LEETCODE_SESSIONandLEETCODE_CSRF(optional but recommended) → these unlock personal "solved / not solved" detection.
How to get the cookies:
Log in to https://leetcode.com in Chrome.
Open DevTools (
F12orCmd+Option+I).Go to Application → Storage → Cookies → https://leetcode.com.
Copy the Value of
LEETCODE_SESSION→ paste into.env.Copy the Value of
csrftoken→ paste asLEETCODE_CSRFin.env.
⚠️ These cookies are like passwords.
.envis already in.gitignoreso it won't be committed. Never share it. They expire every ~2 weeks; just re-copy them when your tools stop seeing your progress.
3. (Optional) quick local test without the AI
source .venv/bin/activate
python smoke_test.py # prints your profile + a few recommendationsPart 4 — Plug it into Cursor
Cursor reads MCP servers from a JSON config. Create/edit
~/.cursor/mcp.json (global) or .cursor/mcp.json in this project, and add:
{
"mcpServers": {
"leetcode": {
"command": "/Users/akshayapratapsingh/Desktop/Leetcode-MCP/.venv/bin/python",
"args": ["/Users/akshayapratapsingh/Desktop/Leetcode-MCP/server.py"]
}
}
}A ready-made copy is in .cursor/mcp.json in this repo already.
Then: Cursor Settings → MCP → you should see leetcode with a green dot and
its 3 tools. Toggle it on. (For Claude Desktop, the same block goes in
claude_desktop_config.json.)
Try it in chat
"Using leetcode, have I solved Two Sum?"
"Recommend 5 medium dynamic programming problems I haven't solved yet."
"What's my LeetCode profile summary?"
"Give me graph problems around difficulty medium, skip ones I've done."
Part 5 — How the code is organized
Leetcode-MCP/
├── server.py # The MCP server: defines the 3 tools (start here)
├── leetcode_client.py # Talks to LeetCode's GraphQL API
├── smoke_test.py # Run the tools directly, no AI needed
├── requirements.txt # Python dependencies
├── .env.example # Template for your username + cookies
├── .cursor/mcp.json # Cursor integration config
└── README.md # This fileReading order to learn: server.py (the tools + decorator) →
leetcode_client.py (the API calls) → smoke_test.py (how to call them).
Part 6 — Ideas to extend it (great for upskilling)
Add
get_daily_challenge(LeetCode's daily problem).Add
get_recent_submissions(username)to show your latest ACs.Add a Resource exposing your solved list as a browsable document.
Cache results so you don't re-hit LeetCode on every call.
Track a "study plan" and recommend the next problem in a curated list.
Troubleshooting
Problem | Fix |
Tools show "solved: null" or note about session | Add valid |
Cursor shows the server red / not connecting | Use absolute paths in |
| Check |
Empty recommendations | Loosen filters (remove topic or difficulty), or set |
403 / errors from LeetCode | Cookies expired — re-copy them from the browser. |
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/akshaya-cp/Leetcode-MCP'
If you have feedback or need assistance with the MCP directory API, please join our Discord server