Motivation
As a daily Cursor IDE user, I'm always looking for ways to enhance my development workflow by integrating the tools and services I use most frequently. Habitify has become an essential part of my daily routine for habit tracking and personal development. Building an MCP server to connect Habitify with Cursor IDE felt like a natural next step - not only would it let me manage my habits without leaving my IDE, but it would also give me hands-on experience with MCP server development.
The MCP (Machine Communication Protocol) specification provides a standardized way to extend Cursor IDE's capabilities. I decided to build this integration to practice implementing production-grade MCP servers while making my personal workflow more efficient.
Implementation
Let's walk through building a production-ready MCP server for Habitify integration. We'll use TypeScript with proper type safety and error handling.
Project Structure
First, let's set up our project structure:
mcp-server/
├── src/
│ ├── server.ts
│ ├── main.ts
│ └── types.ts
├── package.json
├── tsconfig.json
└── .env
Installing Dependencies
Create a new project and install the required dependencies:
mkdir habitify-mcp
cd habitify-mcp
npm init -y
npm install @cursor/mcp-server axios dotenv zod
npm install -D typescript @types/node
Update package.json with the necessary scripts and configuration:
{
"name": "habitify-mcp",
"version": "1.0.0",
"main": "dist/main.js",
"scripts": {
"build": "tsc",
"start": "node dist/main.js"
},
"bin": {
"habitify-mcp": "./dist/main.js"
}
}
Environment Configuration
Create a .env file for API configuration:
HABITIFY_API_KEY=your_api_key_here
HABITIFY_API_URL=https://api.habitify.me/v1
Server Implementation
In src/server.ts
, let's implement our MCP server:
import { McpServer } from '@cursor/mcp-server';
import axios from 'axios';
import { z } from 'zod';
import dotenv from 'dotenv';
// Load environment variables
dotenv.config();
const apiKey = process.env.HABITIFY_API_KEY;
const apiUrl = process.env.HABITIFY_API_URL;
if (!apiKey || !apiUrl) {
throw new Error('Missing required environment variables');
}
// Create axios client with auth
const client = axios.create({
baseURL: apiUrl,
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
});
// Create MCP server instance
export const mcpServer = new McpServer();
// Add tools
mcpServer.tool({
name: 'get_habits',
description: 'Get all habits from Habitify',
parameters: z.object({}),
handler: async () => {
const response = await client.get('/habits');
return response.data;
}
});
mcpServer.tool({
name: 'log_habit',
description: 'Log a habit completion',
parameters: z.object({
habitId: z.string(),
date: z.string(),
value: z.number().optional()
}),
handler: async (params) => {
const response = await client.post(`/logs/${params.habitId}`, {
date: params.date,
value: params.value
});
return response.data;
}
});
Main Entry Point
Create src/main.ts
as the executable entry point:
#!/usr/bin/env node
import { mcpServer } from './server';
mcpServer.listen().catch(console.error);
Usage
Installation
Install the package globally:
npm install -g habitify-mcp
Cursor IDE Configuration
Add the Habitify MCP server to your Cursor IDE configuration by editing ~/.cursor/mcp.json
:
{
"servers": {
"habitify": {
"command": "habitify-mcp"
}
}
}
Example Prompts
Here are some example prompts you can use in Cursor IDE:
- "Show me all my habits"
Response: Here are your current habits:
- Morning Meditation (Daily)
- Exercise (3x per week)
- Read 30 minutes (Daily)
- Code Practice (Weekdays)
- "Mark my meditation habit as complete for today"
Response: ✅ Successfully logged "Morning Meditation" for 2024-01-09
- "Get my habit completion stats for this week"
Response: Weekly Progress:
- Morning Meditation: 3/7 days
- Exercise: 2/3 days
- Read 30 minutes: 5/7 days
- Code Practice: 4/5 days
How It Works
Here's a sequence diagram showing how the Habitify MCP server integrates with Cursor IDE:
Rendering diagram...
The diagram illustrates how user prompts in Cursor IDE are translated into API calls through the MCP server, with responses formatted and returned to the user through Cursor's interface.