This tutorial assumes you have Node.js (v18+) and npm installed, and basic familiarity with TypeScript. You'll also need an AI coding assistant that supports MCP (e.g., Claude Desktop, Cursor).
What is MCP?
Model Context Protocol (MCP) is an open standard that lets AI assistants interact with external tools and data sources. By building a custom MCP server, you can give your AI access to your specific codebase's context—like recent commits, file structure, or code search—without manual copy-pasting.
Step 1: Set Up the Project
Create a new Node.js project with TypeScript:
mkdir my-mcp-server && cd my-mcp-server
npm init -y
npm install @modelcontextprotocol/sdk typescript @types/node --save
npx tsc --initUpdate tsconfig.json to set outDir to ./build and rootDir to ./src.
Step 2: Create the MCP Server
Create src/index.ts with the following skeleton:
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
const server = new Server(
{ name: "my-codebase-tools", version: "1.0.0" },
{ capabilities: { tools: {} } }
);
// We'll add tools here
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("MCP server running on stdio");
}
main().catch(console.error);Step 3: Define a Tool to Get Recent Commits
Add a tool that retrieves the latest 5 commits from Git:
server.setRequestHandler("tools/list", async () => ({
tools: [{
name: "get_recent_commits",
description: "Get the last 5 git commits in the current repository",
inputSchema: {
type: "object",
properties: {}
}
}]
}));
server.setRequestHandler("tools/call", async (request) => {
if (request.params.name === "get_recent_commits") {
const { execSync } = require("child_process");
const commits = execSync("git log --oneline -5").toString();
return { content: [{ type: "text", text: commits }] };
}
throw new Error(`Unknown tool: ${request.params.name}`);
});Step 4: Build and Test
Add a build script in package.json and compile:
// package.json
"scripts": {
"build": "tsc",
"start": "node build/index.js"
}npm run buildTest locally by running the server and sending a mock request:
echo '{"jsonrpc":"2.0","id":1,"method":"tools/list"}' | node build/index.jsYou should see a JSON-RPC response listing your tool. To test tool execution, send a tools/call request with the proper parameters.
Step 5: Connect to Your AI Assistant
For Claude Desktop, add the following to your claude_desktop_config.json:
{
"mcpServers": {
"my-codebase-tools": {
"command": "node",
"args": ["/absolute/path/to/my-mcp-server/build/index.js"],
"env": {}
}
}
}For Cursor, go to Settings > Features > MCP Servers and add the server with the same command.
Now your AI assistant can call get_recent_commits whenever it needs context from your Git history. You can extend this with more tools, such as search_code or get_file_content.
Customization Ideas
- Add a tool that reads the project's README
- Create a tool to list open issues (if using GitHub API)
- Combine tools to give the AI a holistic view of your codebase
Be careful about security: avoid exposing sensitive information (e.g., API keys, environment variables) through your MCP tools.
Interesting approach with MCP. I wonder how much latency this adds to the assistant's responses compared to directly querying git log.