DEV Community

AIRabbit
AIRabbit

Posted on

Cursor MCP Model Context Protocol: The Ultimate Guide to Infinite Context

The biggest bottleneck in AI coding isn't model intelligence—it's context.

Until now, if you wanted Cursor (or any AI editor) to know about your database schema, internal documentation, or live server logs, you had to manually copy-paste that information into the chat window. It was tedious, error-prone, and quickly hit token limits.

Enter the Model Context Protocol (MCP).

In this guide, we'll cover everything you need to know about using MCP in Cursor, from basic configuration to building your own custom MCP servers. We'll go deeper than the official docs, providing working code examples and real-world strategies to supercharge your workflow.

What is the Model Context Protocol (MCP)?

The Model Context Protocol (MCP) is an open standard that enables AI models to "reach out" and interact with external data and tools.

Think of it like a USB port for AI. Instead of trying to stuff the entire internet into the model's training data, MCP provides a standard way for the model to query data on demand.

Why this changes everything for developers:

  • Live Data: Your AI can query your actual Postgres database to see the current schema, not a hallucinated one.
  • Security: You don't paste API keys or sensitive customer data into the chat. The MCP server handles the connection locally.
  • Scalability: You can connect tools that have terabytes of data (like logs or huge docs) without worrying about context window limits. The AI simply searches for what it needs.

Part 1: Setting Up MCP in Cursor

Cursor has built-in support for MCP. It acts as an MCP Client, allowing it to connect to any MCP Server.

1. Prerequisites

  • Cursor version 0.41 or higher.
  • Node.js installed (if you plan to run JavaScript-based servers).
  • Bun (recommended for the fastest local development).

2. The mcp.json Configuration File

Cursor looks for an mcp.json file to know which servers to connect to. You can place this file in two locations:

  • Global: ~/.cursor/mcp.json (Tools available in all projects)
  • Project: .cursor/mcp.json (Tools specific to the current repo)

Here is a robust starter configuration:

{
  "mcpServers": {
    "sqlite": {
      "command": "uvx",
      "args": ["mcp-server-sqlite", "--db-path", "./my-database.db"]
    },
    "filesystem": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-filesystem", "/Users/username/desktop"]
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Pro Tip: Use uvx (from uv) or npx to run servers without manually installing them globally. It keeps your environment clean.

3. Verification

Once configured, open Cursor and go to Cursor Settings > Features > MCP. You should see a list of connected servers with a green "Connected" status.


Part 2: Building a Custom MCP Server (TypeScript)

While pre-built servers are great, the real power comes from building your own. Let's build a practical "DevOps Helper" server. This server will give Cursor the ability to check the status of websites—perfect for debugging connectivity issues without leaving the editor.

Step 1: Initialize the Project

mkdir my-mcp-server
cd my-mcp-server
npm init -y
npm install @modelcontextprotocol/sdk zod
npm install -D typescript @types/node
npx tsc --init
Enter fullscreen mode Exit fullscreen mode

Step 2: Create the Server (index.ts)

We will use the generic McpServer class to define a tool.

#!/usr/bin/env node
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";

// Create the server instance
const server = new McpServer({
  name: "devops-helper",
  version: "1.0.0",
});

// Define a tool: "check_website_status"
server.tool(
  "check_website_status",
  "Checks if a website is reachable and returns its status code and response time.",
  {
    url: z.string().url().describe("The full URL to check (e.g., https://google.com)"),
  },
  async ({ url }) => {
    try {
      const start = Date.now();
      const response = await fetch(url);
      const duration = Date.now() - start;

      return {
        content: [
          {
            type: "text",
            text: `Status: ${response.status} ${response.statusText}\nTime: ${duration}ms`,
          },
        ],
      };
    } catch (error) {
      return {
        content: [
          {
            type: "text",
            text: `Error connecting to ${url}: ${(error as Error).message}`,
          },
        ],
        isError: true,
      };
    }
  }
);

// Connect via Stdio (Standard Input/Output)
async function main() {
  const transport = new StdioServerTransport();
  await server.connect(transport);
  console.error("DevOps Helper MCP Server running on stdio...");
}

main().catch((error) => {
  console.error("Fatal error:", error);
  process.exit(1);
});
Enter fullscreen mode Exit fullscreen mode

Step 3: Build and Configure

Add a build script to your package.json:

"scripts": {
  "build": "tsc",
  "start": "node dist/index.js"
}
Enter fullscreen mode Exit fullscreen mode

Run npm run build.

Step 4: Connect to Cursor

Update your .cursor/mcp.json to point to your new server. Note: Use absolute paths for stability.

{
  "mcpServers": {
    "devops-helper": {
      "command": "node",
      "args": ["/ABSOLUTE/PATH/TO/my-mcp-server/dist/index.js"]
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Now, in Cursor Chat (Cmd+L), you can ask:

"Is https://cursor.com down right now?"

Cursor will automatically call your check_website_status tool and report the result.


Part 3: Advanced Techniques

1. Resources vs. Tools

The example above used a Tool (function execution). MCP also supports Resources (reading data).

  • Use Tools when: You need to perform an action (API call, database write) or calculate something dynamic.
  • Use Resources when: You want to expose static or file-like data (logs, documentation files, database schema).

2. Environment Variables & Security

Never hardcode API keys. Cursor's mcp.json supports environment variable interpolation.

{
  "mcpServers": {
    "github-mcp": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-github"],
      "env": {
        "GITHUB_PERSONAL_ACCESS_TOKEN": "${env:MY_GITHUB_TOKEN}"
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

3. Debugging with MCP Inspector

If your server isn't showing up or acting weird, use the MCP Inspector. It's a web interface to test your server manually.

npx @modelcontextprotocol/inspector node dist/index.js
Enter fullscreen mode Exit fullscreen mode

This launches a UI at localhost:5173 where you can manually invoke tools and see the raw JSON-RPC messages.


Comparison: MCP vs. "Context" Features

Many developers ask: "Why use MCP if Cursor already has @Codebase indexing?"

Feature Cursor @Codebase Cursor + MCP
Data Source Static local files Live databases, APIs, logs, external tools
Freshness Updated on file save Real-time (milliseconds old)
Capability Read-only Can execute actions (create Jira tickets, restart pods)
Privacy Uploads code embeddings Data stays local (for local MCP servers)

Conclusion

The Model Context Protocol transforms Cursor from a smart text editor into a full-stack operational console. By configuring a few simple JSON files, you can give your AI agent permission to check your production DB, read your internal wikis, and debug your APIs—all without leaving the IDE.

Top comments (0)