DEV Community

Battu Bharath Kumar
Battu Bharath Kumar

Posted on

I Built a Tool-Calling AI From Scratch, and Here's What the Bugs Taught Me

I just completed one of the most challenging and rewarding projects of my developer journey: building a complete, end-to-end AI agent that uses a custom tool I built from scratch. It was a rollercoaster of triumphs and bugs, and the story of debugging it is the real lesson. Here’s how it went.

The Goal: An AI Co-writer for Directors

My vision was to create an AI that could help new film directors with creative ideas. I didn't just want a chatbot; I wanted an agent that could use a specialized, custom-built tool to generate unique movie plots on command.

This meant building a full system with two distinct parts:

The Server (The "Doer"): An MCP (Model-Context Protocol) server built with TypeScript and Node.js. Its only job is to host my custom generate_movie_story tool.

The Agent (The "Thinker"): An AI agent script, also in Node.js, that connects to an LLM (I used GPT-3.5-Turbo via OpenRouter) to understand my requests and decide when to call the server.

The Debugging Saga: The Real Learning Experience

Building the basic parts was straightforward, but connecting them was where the real challenge began. Every step of the way, I hit a new, more complex bug.

Round 1: The PowerShell Imposter
My first attempt to test the server with a curl command failed with a bizarre error. After a lot of confusion, I learned that on Windows PowerShell, curl isn't the real curl—it's an alias for Invoke-WebRequest, which has a completely different syntax! This was my first lesson: always know your tools and your environment.

Round 2: The Silent Rejection (406 Error)
With the correct PowerShell syntax, I tried again. This time, the server responded with a 406 Not Acceptable error. This was a huge puzzle. The server was receiving my request but refusing to talk back. The solution? My request was missing an Accept header. I was telling the server what format I was sending (Content-Type), but not what format I could receive. Adding "Accept": "application/json" was a major breakthrough.

Round 3: The Final Boss (When JSON isn't JSON)
Everything seemed perfect. The agent could talk to the LLM, and the LLM decided to call my tool. The agent sent the request... and the app crashed.
Error: Unexpected token 'e', "event: mes"... is not valid JSON

This was the final, most difficult boss. The error meant the server's response wasn't plain JSON. That single "e" was the clue: the response was event: message..., the signature of Server-Sent Events (SSE), a streaming data format. My Node.js agent, expecting a simple JSON object, was crashing because it couldn't parse the stream.

This was the biggest "Aha!" moment. I had to rewrite my agent's networking code to handle the SSE stream, find the line starting with data:, extract the JSON from it, and then parse it.

The Breakthrough: Success!

After fixing the SSE parsing, I ran the whole system one more time. I typed give me a new movie idea... and it worked.

Watching the logs light up as the agent thought, called my server, got the tool's response, and then formulated a completely original movie plot was an incredible moment.

Here is the final, successful output:

You: give me a new movie idea

Thinking...
AI wants to use tool: generate_movie_story
Initializing MCP session...
MCP session initialized: 4d00f927-87ac-47e5-8f6a-7a9f09f8403b
Calling MCP tool: generate_movie_story...
Tool executed successfully

AI: Title: "Shadow of Truth"
Logline: In a world of ancient secrets... a skeptical scientist must uncover a vast conspiracy while facing an ancient curse...

Conclusion

This project taught me that building software isn't about writing perfect code the first time. It's about perseverance and the art of debugging. Every error was a lesson that taught me about PowerShell, HTTP protocols, and AI architecture on a level I never would have learned from a simple tutorial.

You can check out the code for both projects on my GitHub!

The Agent: https://github.com/bharathbattu/MCP_Agent

The Server: https://github.com/bharathbattu/my-first-mcp-server

Thanks for reading my story!

Top comments (0)