LeetCode MCP

LeetCode MCP

A tutorial and working MCP server that connects to your LeetCode profile, enabling AI assistants to check solved problems and recommend new ones by topic and difficulty.

Category
访问服务器

README

LeetCode MCP — Learn MCP by Building One

This project is two things at once:

  1. A gentle, from-scratch tutorial on what an MCP is and how to build one.
  2. 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 server.py (this repo)

An MCP server can expose three kinds of things:

  • Tools — functions the AI can call (e.g. recommend_questions). ← we use this
  • Resources — 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
get_profile Your solved counts (easy/medium/hard) + global ranking.
check_if_solved "Have I solved Two Sum?" → solved / attempted / never tried.
recommend_questions 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.txt

2. Configure your identity + cookies

cp .env.example .env

Then edit .env:

  • LEETCODE_USERNAME → your username from leetcode.com/u/<username>/.
  • LEETCODE_SESSION and LEETCODE_CSRF (optional but recommended) → these unlock personal "solved / not solved" detection.

How to get the cookies:

  1. Log in to https://leetcode.com in Chrome.
  2. Open DevTools (F12 or Cmd+Option+I).
  3. Go to Application → Storage → Cookies → https://leetcode.com.
  4. Copy the Value of LEETCODE_SESSION → paste into .env.
  5. Copy the Value of csrftoken → paste as LEETCODE_CSRF in .env.

⚠️ These cookies are like passwords. .env is already in .gitignore so 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 recommendations

Part 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 file

Reading 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 LEETCODE_SESSION + LEETCODE_CSRF in .env.
Cursor shows the server red / not connecting Use absolute paths in mcp.json; confirm the venv python path exists.
User not found Check LEETCODE_USERNAME matches your profile URL exactly.
Empty recommendations Loosen filters (remove topic or difficulty), or set include_paid=true.
403 / errors from LeetCode Cookies expired — re-copy them from the browser.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选