DEV Community

SoftwareDevs mvpfactory.io
SoftwareDevs mvpfactory.io

Posted on • Originally published at mvpfactory.io

Workshop: Build a 5-Tool MCP Server That Cuts Your AI Token Usage by 95%

What We Are Building

Let me show you a pattern I use in every project now. We are going to build a starter MCP server with five tools that replace the expensive file-by-file crawling LLMs do when they explore your codebase. By the end of this tutorial, you will have a working server that can cut your token usage by 60-75% immediately — and you will understand the architecture to push that to 95%.

The core idea: stop giving models files, start giving them answers.

Prerequisites

  • Node.js 18+
  • A TypeScript project you want to use as your test codebase
  • Basic familiarity with how Claude or GPT tool-calling works
  • The MCP SDK: npm install @modelcontextprotocol/sdk

Step 1: Understand the Problem

Here is what happens every time an LLM explores your repo without structured tooling:

Step What the model reads Tokens burned
package.json Dependencies, scripts ~800
Project structure Directories, entry points ~1,200
6-8 source files Business logic, relationships 15,000-30,000
Config, tests, types Supporting context 10,000+

You are 50K tokens deep before a single useful line of output. That is roughly $0.75 per task on Claude Opus. Do that 20 times a day and you are burning $15 — $450/month.

Step 2: Scaffold the MCP Server

Here is the minimal setup to get this working:

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";

const server = new McpServer({
  name: "codebase-context",
  version: "1.0.0",
});

const transport = new StdioServerTransport();
await server.connect(transport);
Enter fullscreen mode Exit fullscreen mode

That is your running server. Now we add the tools that matter.

Step 3: Build the Five Starter Tools

Tool 1 — Project Summary. This replaces 5-10 file reads with a single 200-token response.

server.tool("get_project_summary", {}, async () => ({
  content: [{
    type: "text",
    text: JSON.stringify({
      stack: "TypeScript, Express, Prisma, PostgreSQL",
      entryPoint: "src/index.ts",
      keyDirs: ["src/services", "src/routes", "src/models"],
      buildCmd: "npm run build",
    }),
  }],
}));
Enter fullscreen mode Exit fullscreen mode

Tool 2 — Dependency Map. Pre-compute module relationships so the model never traces imports.

server.tool("get_dependency_map", { module: z.string() }, async ({ module }) => {
  const graph = await buildDependencyGraph(); // your static analysis
  return {
    content: [{ type: "text", text: JSON.stringify(graph[module]) }],
  };
});
Enter fullscreen mode Exit fullscreen mode

Tool 3 — Relevant Files Finder. Given a task, return only the 3-5 files that need changes.

Tool 4 — Convention Extractor. Naming patterns, error handling style, test structure. Keeps generated code consistent with your codebase.

Tool 5 — Schema/Type Tool. Returns type definitions and API contracts without the model reading full source files.

Tools 3-5 follow the same pattern. Pre-compute the analysis, serve the result as structured JSON.

Step 4: Wire It Into Your Editor

Add this to your Claude config (.claude/mcp.json or equivalent):

{
  "mcpServers": {
    "codebase-context": {
      "command": "node",
      "args": ["./mcp-server/dist/index.js"]
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Now when Claude needs context, it calls your tools instead of reading raw files. Five tool calls returning ~500 tokens total versus 50K tokens of file exploration.

Gotchas

Here is the gotcha that will save you hours: stale metadata is worse than no metadata. If your MCP server returns an outdated architecture summary, the model will generate code that conflicts with your actual codebase. Regenerate your analysis outputs on every commit or CI run.

The docs do not mention this, but tool responses over ~4,000 tokens start losing their advantage. Keep each tool's response tight and structured. If you are returning a full file's contents through a tool, you have missed the point.

One more: do not build 35 tools on day one. The 5-tool starter server delivers the best ROI — 4-8 hours of work for 60-75% token reduction. Scale only after you have measured your baseline.

Wrapping Up

The pattern here is straightforward. Pre-compute what LLMs would otherwise figure out at inference time. Serve structured answers instead of raw files. Measure the difference.

Start this weekend. Build the five tools above. Track your tokens-per-task for a week before and after. The numbers speak for themselves: 52K tokens per task down to 2,600. That is real money back in your pocket and real productivity gained.

For the full MCP specification and advanced patterns, check the official docs.

Top comments (0)