{"slug": "build-a-custom-ai-coding-agent-with-claude-code-and-mcp", "title": "Build a custom AI coding agent with Claude Code and MCP", "summary": "Claude Code, Anthropic's terminal-based CLI coding agent, can be extended with Model Context Protocol (MCP) servers to let the agent query live data such as a PostgreSQL database, according to a hands-on walkthrough. The setup requires Node.js 18 or higher (the author ran v20.11.0) and installs via `npm install -g @anthropic-ai/claude-code`, with a 30-minute debugging session costing about $4.20 in API credits. The author reported the agent traced a NestJS circular dependency between UserService and AuthService in 45 seconds by running `grep -r \"UserService\" src/`, and self-corrected a hallucinated `npm run clean:cache` command to `npm run clear:cache` after reading the resulting error.", "body_md": "# Build a custom AI coding agent with Claude Code and MCP\n\nForget 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.\n\nThat 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.\n\n## Setting up [Claude](/en/tags/claude/) Code on a Mac or Linux box\n\nIf you haven't installed it, you'll need Node.js 18 or higher. I'm running v20.11.0.\n\nRun this to get the CLI:`npm install -g @anthropic-ai/claude-code`\n\nThen initialize it:`claude`\n\nIt 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.\n\n## Connecting an [MCP](/en/tags/mcp/) server for real-time data\n\nThe 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?\n\nI 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):\n\n```\n{\n  \"mcpServers\": {\n    \"postgres\": {\n      \"command\": \"npx\",\n      \"args\": [\"-y\", \"@modelcontextprotocol/server-postgres\", \"postgresql://localhost/my_dev_db\"]\n    }\n  }\n}\n```\n\nOnce 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.\"\n\n## Solving a \"Circular Dependency\" bug without losing my mind\n\nI 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.\n\nI typed:`Find the circular dependency between UserService and AuthService, trace the imports, and suggest a refactor using a forward ref or a shared module.`\n\nThe 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.\n\nThe fix it suggested was a simple move of the interface to a `types.ts` file.\n\n``` python\n// Before: circular import\nimport { UserService } from './user.service'; \n\n// After: import from a neutral leaf node\nimport { UserType } from './types';\n```\n\nIt took 45 seconds. Doing that manually would have taken me 20 minutes of clicking through tabs.\n\n## When the agent hallucinates a command\n\nIt'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.\n\nThe 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`.\n\nIf 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.\n\n## Why you need an AI technology community like PromptCube\n\nWorking 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.\n\nPromptCube 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.\n\nYou 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.\n\n## Comparing the local AI experience\n\nI've tried [Cursor](/en/tags/cursor/), Windsurf, and Claude Code. Here is the raw truth from my experience:\n\n| Feature | Cursor/Windsurf | Claude Code (CLI) |\n\n| :--- | :--- | :--- |\n\n| **Context** | Great (Index-based) | Better (Real-time shell access) |\n\n| **Execution** | Terminal is separate | Terminal *is* the interface |\n\n| **Speed** | Fast UI | Fast iteration on file changes |\n\n| **Risk** | Low (You accept changes) | Moderate (It can run `rm -rf` if you're not watching) |\n\nIf 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.\n\n## My recommended starting stack\n\nIf you're starting today, don't overcomplicate it. Use this setup:\n\n1. **Claude Code** for heavy lifting and refactoring.\n\n2. **MCP Postgres/Filesystem servers** to give the AI sight.\n\n3. **PromptCube** to stop guessing which prompts work for complex TypeScript logic.\n\nStop 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.\n\n[Next Paul Ford is right that AI makes it too easy to do a job badly →](/en/news/9274/)", "url": "https://wpnews.pro/news/build-a-custom-ai-coding-agent-with-claude-code-and-mcp", "canonical_source": "https://promptcube3.com/en/posts/9277/", "published_at": "2026-09-12 20:26:05+00:00", "updated_at": "2026-09-12 20:55:00.200580+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools", "ai-products"], "entities": ["Claude Code", "Anthropic", "Model Context Protocol", "PostgreSQL", "NestJS", "Node.js", "Promp"], "alternates": {"html": "https://wpnews.pro/news/build-a-custom-ai-coding-agent-with-claude-code-and-mcp", "markdown": "https://wpnews.pro/news/build-a-custom-ai-coding-agent-with-claude-code-and-mcp.md", "text": "https://wpnews.pro/news/build-a-custom-ai-coding-agent-with-claude-code-and-mcp.txt", "jsonld": "https://wpnews.pro/news/build-a-custom-ai-coding-agent-with-claude-code-and-mcp.jsonld"}}