LeetCode每日问题服务

一个提供LeetCode每日问题的HTTP服务,支持通过MCP协议与编辑器集成,适用于编程学习和开发工具增强。

  • by iamgovindthakur
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 iamgovindthakur/leetcode_mcp_server

使用说明

LeetCode MCP示例

MCP风格HTTP服务器暴露的最小示例 GET /daily 返回LeetCode“每日问题”(标题、url、代码片段)。此项目是一个小型演示,可用于编辑器集成或作为功能实验的模板。

特性

  • FastAPI应用程序 leetcode_mcp 包裹
  • 获取策略:先使用GraphQL,然后进行主页解析回退
  • 通过环境变量进行配置(LC_USER_AGENT, LC_TIMEOUT)
  • 测试使用 pytest 和 respx (HTTP模拟)

先决条件

  • Python 3.10+(CI中使用3.12)
  • Docker(可选,如果你想运行容器)

快速入门(本地)

  1. 创建并激活虚拟环境(macOS/zsh):
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
  1. 在本地运行服务器(包入口点):
uvicorn leetcode_mcp:app --host 127.0.0.1 --port 8080

您还可以使用 Makefile:

make install   # creates .venv and installs deps
make run       # starts uvicorn on 127.0.0.1:8080

健康检查

curl http://127.0.0.1:8080/health

解决日常问题

curl http://127.0.0.1:8080/daily

使用Docker运行

docker build -t leetcode-mcp:latest .
docker run --rm -p 8080:8080 leetcode-mcp:latest

测试

从存储库根运行测试(确保 PYTHONPATH=. 或激活venv):

source .venv/bin/activate
PYTHONPATH=. pytest -q

与MCP客户端(如Copilot)集成

您可以将正在运行的服务器公开为MCP HTTP服务器,以便编辑器工具(支持MCP)(包括Copilot风格的集成)可以调用 GET /daily 以获得当天的问题。

  1. 在本地启动服务器(示例):
# using the Makefile
make run

# or directly
uvicorn leetcode_mcp:app --host 127.0.0.1 --port 8080
  1. 确认端点可访问:
curl http://127.0.0.1:8080/daily
  1. 配置编辑器MCP文件,以便MCP感知客户端可以发现服务器。示例 .vscode/mcp.json (已包含在本回购协议中 .vscode/mcp.json):
{
	"servers": {
		"leetcode-mcp": {
			"type": "http",
			"url": "http://127.0.0.1:8080/daily"
		}
	}
}
  1. 重新加载编辑器(或MCP扩展),使其获取新的MCP服务器配置。确切的步骤取决于您使用的MCP客户端或Copilot集成——如果发现不是自动发生的,请查阅该扩展的文档。

快速测试:从编辑器的MCP客户端UI(或通过任何支持MCP的扩展)中,请求 leetcode-mcp 服务器并验证您是否收到JSON响应。

安全和网络注意事项

  • 上述设置旨在用于本地开发。如果在网络接口上公开端点,请在允许远程访问之前对其进行适当的保护(防火墙、身份验证)。
  • 如果在容器中运行。, -p 8080:8080)并在中使用适当的主机URL .vscode/mcp.json.
  • 如果您的MCP客户端是远程的(例如,基于云的开发环境),您可能需要通过SSH隧道或类似ngrok的安全隧道公开服务,并相应地更新MCP URL。

GitHub Copilot(MCP)集成和示例使用

这个项目主要是演示小型MCP HTTP服务器如何通过公开结构化数据来增强编辑器助手功能(例如GitHub Copilot或其他支持MCP的助手)。下面的步骤显示了如何将正在运行的服务器连接到启用MCP的Copilot集成中,以及一个示例提示+预期输出,您可以使用它来验证行为。

与具有Copilot/MCP功能的编辑器集成的步骤

  1. 在本地启动MCP服务器:
# start the server
make run
  1. 确保MCP发现配置存在于您的工作区中(此仓库包括 .vscode/mcp.json):
{
	"servers": {
		"leetcode-mcp": {
			"type": "http",
			"url": "http://127.0.0.1:8080/daily"
		}
	}
}
  1. 重新加载编辑器或Copilot/MCP扩展,使其拾取新的服务器条目。

  2. 打开Copilot Chat(或您的MCP客户端)并致电 leetcode-mcp 服务器(具体的UI因扩展而异)。如果服务器可访问,您应该看到JSON响应,助手可以使用它来丰富其输出。

示例使用(提示+预期的助手输出)

使用此示例验证端到端集成。它假设Copilot(或您的MCP客户端)可以调用注册的MCP服务器,并将返回的JSON合并到辅助响应中。

示例提示(您给副驾驶/MCP的输入):

Fetch today's LeetCode problem from the registered MCP server `leetcode-mcp` and produce:

1) A one-paragraph plain-English summary of the problem.
2) A concise algorithmic approach and complexity (time/space).
3) A short, correct Python function implementing the solution.

Please reference the problem title and URL in your answer.

示例预期输出(助理可能返回的内容):

Title: Find Minimum Operations to Make All Elements Divisible by Three
URL: https://leetcode.com/problems/find-minimum-operations-to-make-all-elements-divisible-by-three/

Summary:
You are given an integer array `nums`. In one operation you can add or subtract 1 from any element. The goal is to make every element divisible by 3 using the minimum number of operations.

Approach:
Compute the remainder of each element modulo 3. Elements with remainder 0 need no change. Each element with remainder 1 or 2 can be fixed with one operation (+1 or -1 respectively) to become divisible by 3. Therefore the minimum number of operations equals the count of elements whose `x % 3 != 0`.

Complexity:
Time: O(n) — single pass to count non-multiples of 3.
Space: O(1) — constant extra space.

Python implementation:
```python
from typing import List

def min_operations_make_div_by_three(nums: List[int]) -> int:
		return sum(1 for x in nums if x % 3 != 0)

# Example
print(min_operations_make_div_by_three([1,2,3,4]))  # 3

笔记:

  • 此示例输出有意简洁,因此助手可以将其用作代码片段,以增加完成度,或根据需要生成更完整的解释或代码演练。
  • 助理应该更喜欢链接到规范 URL 而不是逐字复制长问题陈述。

Troubleshooting

- If Copilot (or the MCP client) does not show the server, confirm the server is running and that `.vscode/mcp.json` points to the correct URL. Some MCP integrations require an editor restart.
- Check the extension or editor logs to see the raw HTTP response if the assistant output does not match expectations.