LE

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.

Quick Install
npm install

Overview

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):

  1. Clone and install
git clone https://github.com/doggybee/mcp-server-leetcode.git
cd mcp-server-leetcode
npm install
  1. Configure environment variables (see table below), then run:
# Example using an environment file
export PORT=3000
export LEETCODE_SESSION="<your_leetcode_session_cookie_here>"

npm run start
# or
node dist/index.js

Docker

# Build and run
docker build -t mcp-leetcode .
docker run -p 3000:3000 -e LEETCODE_SESSION="<your_cookie>" mcp-leetcode

Common environment variables

VariableDescriptionExample
PORTHTTP port the GraphQL server listens on3000
LEETCODE_SESSION(Optional) LeetCode session cookie for authenticated requests“LEETCODE_SESSION=abc123”
LOG_LEVELVerbosity level for server logsinfo, 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

  1. Query a problem for an LLM assistant
  • Fetch the problem statement, difficulty, and tags to provide context for code generation.
curl -s -X POST http://localhost:3000/graphql \
  -H "Content-Type: application/json" \
  -d '{"query":"query($s:String!){ problem(slug:$s){ title difficulty content tags } }","variables":{"s":"two-sum"}}'
  1. Build a personal practice dashboard
  • Use authenticated requests to list a user’s solved problems and recent submission outcomes.
query userSolved($u:String!){
  user(username:$u){
    username
    solvedCount
    recentSubmissions(limit:5){
      problem{title,slug}
      status
      runtime
      memory
      timestamp
    }
  }
}
  1. Contest aggregator or notifier
  • Pull upcoming contest metadata and feed it to a calendar or notification system.
query {
  contest(id: "weekly-contest-123") {
    id
    title
    startTime
    duration
    problems { title, slug, difficulty }
  }
}

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.