LeetCode MCP Server GraphQL API Access
Access LeetCode problem, user, and contest data via the MCP server using GraphQL to integrate and query resources securely.
npm installOverview
The LeetCode MCP Server GraphQL API exposes LeetCode problem, user, and contest data through a Model Context Protocol (MCP) server using a GraphQL endpoint. It acts as a secure, local proxy that normalizes LeetCode resources into a consistent GraphQL schema so developer tools, bots, or LLMs can request contextual information (problems, user profiles, contest metadata) without scraping HTML directly or embedding credentials in client code.
This server is useful when you need programmatic access to LeetCode data for integrations such as code assistants, study dashboards, or contest aggregators. Running the MCP server locally or in a trusted environment keeps your LeetCode session credentials secure while allowing any client that understands GraphQL (curl, Apollo, fetch, or an LLM integration) to query exactly the fields needed.
Features
- GraphQL endpoint that exposes LeetCode resources: Problem, User, Contest (and related fields)
- MCP-compatible: designed for integration with model-context protocols and assistants
- Supports authenticated access for user-specific data (submissions, private contest info) via environment credentials
- Lightweight server suitable for local development or deployment in private infrastructure
- Simple JSON GraphQL API usable from any language or tool that supports HTTP requests
Installation / Configuration
Quick start (Node.js-based repo):
- Clone and install
- Configure environment variables (see table below), then run:
# Example using an environment file
# or
Docker
# Build and run
Common environment variables
| Variable | Description | Example |
|---|---|---|
| PORT | HTTP port the GraphQL server listens on | 3000 |
| LEETCODE_SESSION | (Optional) LeetCode session cookie for authenticated requests | “LEETCODE_SESSION=abc123” |
| LOG_LEVEL | Verbosity level for server logs | info, debug, warn |
Notes
- LEETCODE_SESSION (or equivalent cookie env) is only required if you want to access user-specific endpoints (like your own submissions or private contest info). The server will still provide public problem metadata without authentication.
- Keep session cookies secret. Run the server in a trusted environment when providing credentials.
Available Resources
The server exposes a GraphQL schema tailored to common LeetCode data. Typical top-level queries include:
- problem(slug: String!): Problem
- user(username: String!): User
- contest(id: ID!): Contest
- problems(filter: ProblemFilter, limit: Int): [Problem]
Common types and fields:
- Problem
- id, title, slug, difficulty, content (HTML/markdown), likes, dislikes
- stats: totalAccepted, totalSubmissions
- tags: [String]
- User
- username, reputation, badges, solvedCount, recentSubmissions
- Contest
- id, title, startTime, duration, problems
Schema details can be introspected by hitting the /graphql endpoint with a standard GraphQL introspection query.
Use Cases
- Query a problem for an LLM assistant
- Fetch the problem statement, difficulty, and tags to provide context for code generation.
- Build a personal practice dashboard
- Use authenticated requests to list a user’s solved problems and recent submission outcomes.
query userSolved($u:String!)
- Contest aggregator or notifier
- Pull upcoming contest metadata and feed it to a calendar or notification system.
query
Best Practices & Security
- Never commit session cookies or credentials to source control. Use environment variables or a secrets manager.
- Expose the server only to trusted clients (localhost or private network) when using authentication.
- Use GraphQL queries that request only necessary fields to minimize data exposure.
- Periodically rotate LeetCode session cookies and revoke tokens if compromised.
Troubleshooting
- 401/403 responses: confirm LEETCODE_SESSION is valid and not expired (used for private endpoints).
- Empty problem content: some problems may be behind paid content gates; ensure your account has appropriate access.
- CORS or network issues: when calling from a browser-based client, configure CORS in the server or proxy through a backend.
This MCP GraphQL server simplifies integrating LeetCode data into tools and model contexts by providing a focused, secure, and queryable API tailored for developer workflows.