# Build a custom AI coding agent with Claude Code and MCP

> Source: <https://promptcube3.com/en/posts/9277/>
> Published: 2026-09-12 20:26:05+00:00

# Build a custom AI coding agent with Claude Code and MCP

Forget the hype about "autonomous agents" for a second. I spent last Thursday fighting with a legacy TypeScript codebase that had zero documentation and a dependency tree from 2019. Standard Copilot autocomplete wasn't cutting it because the context window couldn't see the weird side effects happening in three different directories. I needed a tool that could actually *run* shell commands, read files, and iterate on its own mistakes without me copy-pasting logs back and forth.

That is where [Claude Code](/en/tags/claude%20code/) and the Model Context Protocol (MCP) actually change the game. Instead of a chat box, you get a CLI that lives inside your terminal and can actually touch your filesystem.

## Setting up [Claude](/en/tags/claude/) Code on a Mac or Linux box

If you haven't installed it, you'll need Node.js 18 or higher. I'm running v20.11.0.

Run this to get the CLI:`npm install -g @anthropic-ai/claude-code`

Then initialize it:`claude`

It will ask you to authenticate with your Anthropic account. Be warned: this isn't free. It eats tokens fast because it's constantly reading your file structure to understand where it is. On a medium-sized project, a 30-minute debugging session cost me about $4.20 in API credits. That's the price for not having to manually explain the folder structure for the tenth time.

## Connecting an [MCP](/en/tags/mcp/) server for real-time data

The real power isn't the LLM; it's the "hands" you give it via MCP. By default, Claude can read files. But what if you want it to query your actual database to see why a specific user record is corrupted?

I used the PostgreSQL MCP server to let the agent inspect my schema. Here is how you add a server to your config (usually located in `~/Library/Application Support/claude-code/config.json` on Mac):

```
{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-postgres", "postgresql://localhost/my_dev_db"]
    }
  }
}
```

Once you restart the CLI, you can literally tell the agent: "Check the users table and tell me why the email for ID 402 is null." It will execute the SQL, read the result, and then tell you which line of code in your API is failing to validate the input. This closes the loop between "guessing" and "knowing."

## Solving a "Circular Dependency" bug without losing my mind

I hit a wall last week with a `Circular dependency` error in a NestJS project. The logs were vague. Instead of hunting through 40 files, I gave Claude Code a specific set of instructions.

I typed:`Find the circular dependency between UserService and AuthService, trace the imports, and suggest a refactor using a forward ref or a shared module.`

The agent did something interesting. It didn't just suggest code. It ran `grep -r "UserService" src/` to map every single import. It found a hidden import in a `user.entity.ts` file that I had completely forgotten about.

The fix it suggested was a simple move of the interface to a `types.ts` file.

``` python
// Before: circular import
import { UserService } from './user.service'; 

// After: import from a neutral leaf node
import { UserType } from './types';
```

It took 45 seconds. Doing that manually would have taken me 20 minutes of clicking through tabs.

## When the agent hallucinates a command

It's not perfect. At one point, the agent tried to run `npm run clean:cache`, which doesn't exist in my `package.json`. It got a `command not found` error.

The wild part is how it handles failure. It read the error, ran `cat package.json`, realized it had hallucinated the script name, and then corrected itself to `npm run clear:cache`.

If you find the agent looping on a mistake, don't keep saying "try again." Give it a hint. Tell it: "Check the scripts section of package.json first." This forces it to ground its logic in the actual filesystem before guessing.

## Why you need an AI technology community like PromptCube

Working in a vacuum is the slowest way to learn. I used to spend hours tweaking a prompt to get the LLM to stop adding "Here is the code..." fluff to every response. Then I joined the PromptCube community and realized people had already solved this using specific system prompts and versioned templates.

PromptCube isn't just a place to share prompts; it's where you find out which MCP servers actually work and which ones crash your terminal. When you join, you get access to a collective brain of devs who are hitting the same "context window full" walls you are.

You can dive into different [Workflows](/en/category/workflows/) to see how other seniors are chaining Claude Code with GitHub Actions or using it for automated PR reviews. It turns the "trial and error" phase of AI coding into a "copy and refine" phase.

## Comparing the local AI experience

I've tried [Cursor](/en/tags/cursor/), Windsurf, and Claude Code. Here is the raw truth from my experience:

| Feature | Cursor/Windsurf | Claude Code (CLI) |

| :--- | :--- | :--- |

| **Context** | Great (Index-based) | Better (Real-time shell access) |

| **Execution** | Terminal is separate | Terminal *is* the interface |

| **Speed** | Fast UI | Fast iteration on file changes |

| **Risk** | Low (You accept changes) | Moderate (It can run `rm -rf` if you're not watching) |

If you want a cozy IDE experience, stay with Cursor. If you are a terminal rat who wants an agent that can actually "operate" the machine, go with the CLI.

## My recommended starting stack

If you're starting today, don't overcomplicate it. Use this setup:

1. **Claude Code** for heavy lifting and refactoring.

2. **MCP Postgres/Filesystem servers** to give the AI sight.

3. **PromptCube** to stop guessing which prompts work for complex TypeScript logic.

Stop treating the AI like a search engine. Treat it like a junior dev who is incredibly fast but occasionally forgets where he is. Give it tools, give it a restricted scope, and for god's sake, keep your git commits frequent so you can revert when it decides to "optimize" your entire auth flow into a single 500-line function.

[Next Paul Ford is right that AI makes it too easy to do a job badly →](/en/news/9274/)
