LeetCode MCP Server
An MCP server that provides tools to interact with the LeetCode API, enabling problem fetching, code template generation, and solution execution/submission.
README
LeetCode MCP Server
An MCP-compatible server that exposes a set of tools to interact with the LeetCode API. This allows you to fetch problems, generate code templates, run, and submit solutions programmatically.
Overview
This project provides a simple and effective way to interface with LeetCode's services. It's built using Python and the mcp framework, exposing functionalities as tools that can be easily integrated into other applications or used for automating LeetCode tasks.
Features
- Fetch Problem Details: Get the full description, examples, and constraints for any LeetCode problem.
- Code Template Generation: Generate starter code for any problem in your preferred language.
- Run Code: Test your solution against the example test cases.
- Submit Code: Submit your solution for evaluation against the full test suite.
- Daily Challenge: Fetch the current daily LeetCode challenge.
- Search Problems: Search for problems with various filters like tags, difficulty, and keywords.
Getting Started
Prerequisites
- Python 3.12+
- Docker (for containerized deployment)
- Git
Configuration
To interact with the LeetCode API, you need to provide your LEETCODE_SESSION and LEETCODE_CSRFTOKEN cookies.
- Log in to your LeetCode account in your web browser.
- Open the developer tools (usually by pressing
F12orCtrl+Shift+I). - Go to the Application (or Storage) tab.
- Find the Cookies section and select
https://leetcode.com. - Locate the
LEETCODE_SESSIONandcsrftokencookies and copy their values.
Create a .env file in the root of the project and add your credentials:
# .env
LEETCODE_SESSION=your_leetcode_session_cookie
LEETCODE_CSRFTOKEN=your_leetcode_csrftoken
Local Installation
-
Clone the repository:
git clone https://github.com/AHM215/leetcode-mcp.git cd leetcode-mcp -
Install dependencies:
pip install uv uv pip install -e . -
Run the server:
uv run python run_server.pyThe server will start, and you can interact with it using an MCP client.
Docker Usage
Pull from Docker Hub
You can pull the pre-built Docker image from Docker Hub.
docker pull ahm215/leetcode-mcp-server:latest
Run with Docker
To run the server using Docker, you need to pass your LeetCode credentials as environment variables.
docker run -d \
-p 8000:8000 \
--name leetcode-mcp-server \
-e LEETCODE_SESSION="your_leetcode_session_cookie" \
-e LEETCODE_CSRFTOKEN="your_leetcode_csrftoken" \
ahm215/leetcode-mcp-server:latest
Build from Source
You can also build the Docker image from the source code.
- Clone the repository (if you haven't already).
- Build the image:
docker build -t leetcode-mcp-server . - Run the container as shown in the "Run with Docker" section, using
leetcode-mcp-serveras the image name.
Claude Desktop Usage
If you want to run this server as a tool in a Claude Desktop environment, you can use the following JSON configuration. This defines a tool that runs the Docker container and passes the necessary credentials.
Note: You must replace the placeholder values for --leetcode-session and --csrftoken with your actual LeetCode cookies.
"docker-leetcode-mcp-server": {
"type": "stdio",
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"ahm215/leetcode-mcp-server:v1.0.0",
"--leetcode-session",
"your_leetcode_session_cookie",
"--csrftoken",
"your_leetcode_csrftoken"
]
}
Available Tools (API)
The server exposes the following tools:
fetch_problem_plain_text
Fetches the plain text content of a LeetCode problem given its URL.
- Parameters:
link(str): The full URL of the LeetCode problem.
- Returns: (str) The plain text description of the problem.
generate_template
Generates a language-specific code template for a given LeetCode problem.
- Parameters:
problem_slug(str): The slug of the problem from its URL (e.g., "two-sum").code_lang(str): The language slug (e.g., "python3", "java", "cpp").
- Returns: (str) The code template.
run_code
Executes code against the example test cases for a LeetCode problem.
- Parameters:
problem_slug(str): The slug of the problem.code_lang(str): The language slug.code(str): The code to be executed.
- Returns: (dict) The results of the execution.
submit_code
Submits code for a LeetCode problem for evaluation against the full test suite.
- Parameters:
problem_slug(str): The slug of the problem.code_lang(str): The language slug.code(str): The code to be submitted.
- Returns: (dict) The submission result.
get_daily_challenge
Retrieves today's LeetCode Daily Challenge problem.
- Parameters: None
- Returns: (dict) Details of the daily challenge problem.
get_problem
Retrieves details about a specific LeetCode problem.
- Parameters:
titleSlug(str): The slug of the problem.
- Returns: (dict) Detailed information about the problem.
search_problems
Searches for LeetCode problems based on various filters.
- Parameters:
category(str, optional): The category to search in. Defaults to "all-code-essentials".tags(List[str], optional): A list of tags to filter by.difficulty(str, optional): The difficulty level ("EASY", "MEDIUM", "HARD").searchKeywords(str, optional): Keywords to search for.limit(int, optional): The number of results to return. Defaults to 10.offset(int, optional): The offset for pagination. Defaults to 0.
- Returns: (dict) A list of problems matching the criteria.
Contributing
Contributions are welcome! Please feel free to submit a pull request or open an issue if you have any suggestions or find any bugs.
- Fork the repository.
- Create your feature branch (
git checkout -b feature/AmazingFeature). - Commit your changes (
git commit -m 'Add some AmazingFeature'). - Push to the branch (
git push origin feature/AmazingFeature). - Open a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。