<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Battu Bharath Kumar</title>
    <description>The latest articles on Forem by Battu Bharath Kumar (@bharathbattu).</description>
    <link>https://forem.com/bharathbattu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3558080%2F40f20379-7371-4d80-a914-2a3432f1a614.jpeg</url>
      <title>Forem: Battu Bharath Kumar</title>
      <link>https://forem.com/bharathbattu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/bharathbattu"/>
    <language>en</language>
    <item>
      <title>I Built a Tool-Calling AI From Scratch, and Here's What the Bugs Taught Me</title>
      <dc:creator>Battu Bharath Kumar</dc:creator>
      <pubDate>Sat, 11 Oct 2025 03:49:33 +0000</pubDate>
      <link>https://forem.com/bharathbattu/i-built-a-tool-calling-ai-from-scratch-and-heres-what-the-bugs-taught-me-23ip</link>
      <guid>https://forem.com/bharathbattu/i-built-a-tool-calling-ai-from-scratch-and-heres-what-the-bugs-taught-me-23ip</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Goal: An AI Co-writer for Directors
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;This meant building a full system with two distinct parts:&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Debugging Saga: The Real Learning Experience
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Round 1: The PowerShell Imposter&lt;br&gt;
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.&lt;/p&gt;

&lt;p&gt;Round 2: The Silent Rejection (406 Error)&lt;br&gt;
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.&lt;/p&gt;

&lt;p&gt;Round 3: The Final Boss (When JSON isn't JSON)&lt;br&gt;
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.&lt;br&gt;
Error: Unexpected token 'e', "event: mes"... is not valid JSON&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Breakthrough: Success!
&lt;/h2&gt;

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

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Here is the final, successful output:&lt;/p&gt;

&lt;p&gt;You: give me a new movie idea&lt;/p&gt;

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

&lt;p&gt;AI: Title: "Shadow of Truth"&lt;br&gt;
Logline: In a world of ancient secrets... a skeptical scientist must uncover a vast conspiracy while facing an ancient curse...&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdb6mexhdf2rv5okvfasg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdb6mexhdf2rv5okvfasg.png" alt=" " width="784" height="650"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;You can check out the code for both projects on my GitHub!&lt;/p&gt;

&lt;p&gt;The Agent: &lt;a href="https://github.com/bharathbattu/MCP_Agent" rel="noopener noreferrer"&gt;https://github.com/bharathbattu/MCP_Agent&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Server: &lt;a href="https://github.com/bharathbattu/my-first-mcp-server" rel="noopener noreferrer"&gt;https://github.com/bharathbattu/my-first-mcp-server&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading my story!&lt;/p&gt;

</description>
      <category>mcp</category>
      <category>ai</category>
      <category>learning</category>
      <category>llm</category>
    </item>
  </channel>
</rss>
