← Back to Series Course 4 of 14

Build Your Own Tool System

AI can think. But it can't DO anything — until you give it tools.

In 30 minutes you'll build a tool system, create a skill, and understand MCP.

For absolute beginners. No assumed knowledge.

Chapter 1: The Helpless Genius

Ask any AI model to write you a sonnet about quantum physics. It'll do it beautifully. Ask it what time it is. It can't tell you.

Try This Right Now

An LLM without tools is a brain without hands. Let's prove it.

Hi! I'm a language model. I can write, reason, and chat — but I can't DO anything in the real world. Try asking me something I can't answer!

The AI can talk about math, but it can't actually calculate. It can describe weather patterns, but can't check today's forecast. It knows about files and databases, but can't read your documents.

Pure language models are brilliant but helpless.

📚 Go Deeper

GPT-4 Paper — Why even the most advanced models need tools

Chapter 2: Function Calling

Function calling is how you give AI hands. You describe a tool as a JSON schema. The model reads the schema and decides when to call it.

The Calculator Tool

Let's build the simplest possible tool: a calculator.

const calculatorTool = {
  type: 'function',
  function: {
    name: 'calculator',
    description: 'Evaluate a math expression like 847 * 293',
    parameters: {
      type: 'object',
      properties: {
        expression: {
          type: 'string',
          description: 'The math expression to evaluate'
        }
      },
      required: ['expression']
    }
  }
};

// The actual function that does the work
function calculator({ expression }) {
  try {
    return eval(expression);  // Don't use eval in production!
  } catch (error) {
    return `Error: ${error.message}`;
  }
}

The JSON Dance

Here's what happens when the AI wants to use your tool:

// 1. You send the tool description with your message
{
  "model": "gpt-4o-mini",
  "messages": [{"role": "user", "content": "What's 847 × 293?"}],
  "tools": [calculatorTool]
}

// 2. The AI responds with a tool_call (not text!)
{
  "role": "assistant",
  "tool_calls": [{
    "id": "call_abc123",
    "type": "function",
    "function": {
      "name": "calculator",
      "arguments": "{\"expression\": \"847 * 293\"}"
    }
  }]
}

// 3. You execute the function and send the result back
{
  "role": "tool",
  "tool_call_id": "call_abc123",
  "content": "248071"
}

// 4. The AI sees the result and responds normally
{
  "role": "assistant",
  "content": "847 × 293 = 248,071"
}

The AI never sees your code. It only sees the JSON description. If your description is wrong, the AI will use the tool incorrectly.

📚 Go Deeper

OpenAI Function Calling Guide — Complete documentation

Chapter 3: The ReAct Loop

What happens when the model needs multiple tools? Or needs to call the same tool twice? Welcome to the ReAct loop: Reason → Act → Observe → Repeat.

🔗 Connection: This is the same loop you'll build in Build Your Own Agent — but there it connects to real APIs. The Agent course is the capstone that ties tools, memory, and security together.

The Loop Visualization

🤔
Think
Agent reads the message, plans what to do
🔧
Act
Agent calls one or more tools
👁
Observe
We execute tools, return results
🔄
Repeat
Loop until task is complete

Multi-Tool Agent

Let's build an agent with multiple tools: calculator, clock, and weather.

const tools = [
  {
    type: 'function',
    function: {
      name: 'calculator',
      description: 'Evaluate math expressions',
      parameters: {
        type: 'object',
        properties: {
          expression: { type: 'string' }
        },
        required: ['expression']
      }
    }
  },
  {
    type: 'function',
    function: {
      name: 'get_current_time',
      description: 'Get the current time',
      parameters: { type: 'object', properties: {} }
    }
  },
  {
    type: 'function',
    function: {
      name: 'get_weather',
      description: 'Get weather for a city',
      parameters: {
        type: 'object',
        properties: {
          city: { type: 'string' }
        },
        required: ['city']
      }
    }
  }
];

// The agent loop that handles multiple tool calls
async function agentLoop(userMessage) {
  const messages = [{ role: 'user', content: userMessage }];
  
  while (true) {
    const response = await fetch(API_ENDPOINT, {
      method: 'POST',
      headers: { /* auth headers */ },
      body: JSON.stringify({
        model: 'gpt-4o-mini',
        messages: messages,
        tools: tools
      })
    });
    
    const data = await response.json();
    const message = data.choices[0].message;
    messages.push(message);
    
    // If no tool calls, we're done
    if (!message.tool_calls) {
      return message.content;
    }
    
    // Execute each tool call
    for (const toolCall of message.tool_calls) {
      const result = executeFunction(toolCall.function.name, toolCall.function.arguments);
      messages.push({
        role: 'tool',
        tool_call_id: toolCall.id,
        content: result
      });
    }
    
    // Loop back — model will see the results and decide what to do next
  }
}

📚 Go Deeper

ReAct Paper — The original research on reasoning and acting

Chapter 4: Building a Skill

A tool is one function. A skill is a package: multiple tools + instructions + context.

File Manager Skill

Let's build a complete file management skill with multiple related tools.

📁 File Manager Skill

Tools in this skill:
  • read_file — Read contents of a file
  • write_file — Write content to a file
  • list_directory — List files in a directory
  • search_files — Search for files by name or content
// SKILL.md — Instructions for the AI agent
/*
# File Manager Skill

## Purpose
This skill gives you complete file system access. Use it to help users organize, read, and manipulate files.

## When to use
- User asks about files or directories
- User wants to read, write, or search files
- User needs help organizing their filesystem

## Guidelines
- Always use list_directory before trying to read files
- Ask for confirmation before writing/deleting files
- Use relative paths when possible
- Be helpful but respectful of user's file structure
*/

const fileManagerTools = [
  {
    type: 'function',
    function: {
      name: 'read_file',
      description: 'Read the contents of a file',
      parameters: {
        type: 'object',
        properties: {
          filepath: { type: 'string', description: 'Path to the file to read' }
        },
        required: ['filepath']
      }
    }
  },
  {
    type: 'function',
    function: {
      name: 'write_file',
      description: 'Write content to a file',
      parameters: {
        type: 'object',
        properties: {
          filepath: { type: 'string', description: 'Path to write to' },
          content: { type: 'string', description: 'Content to write' }
        },
        required: ['filepath', 'content']
      }
    }
  },
  {
    type: 'function',
    function: {
      name: 'list_directory',
      description: 'List files and folders in a directory',
      parameters: {
        type: 'object',
        properties: {
          path: { type: 'string', description: 'Directory path to list' }
        },
        required: ['path']
      }
    }
  },
  {
    type: 'function',
    function: {
      name: 'search_files',
      description: 'Search for files by name or content',
      parameters: {
        type: 'object',
        properties: {
          query: { type: 'string', description: 'Search query' },
          directory: { type: 'string', description: 'Directory to search in' }
        },
        required: ['query']
      }
    }
  }
];

Skills are how you teach an agent a new job. The SKILL.md file is like a job description — it tells the agent when and how to use the tools.

📚 Go Deeper

LangChain Tools — How frameworks organize tools into skills

Chapter 5: MCP — The Universal Plug

Model Context Protocol is USB-C for AI tools. One standard way for any AI to connect to any tool server.

The Problem MCP Solves

Before MCP: N×M Integration Hell

GPT-4
Claude
Gemini
Custom API
Different API
Another API
Calendar
Email
Files

Every model needs custom integration with every tool = 9 different APIs

After MCP: N+M Standard Connection

GPT-4
Claude
Gemini
MCP
MCP
MCP
Calendar
Email
Files

Every model speaks MCP = one standard, everything connects

Building an MCP Server

MCP is JSON-RPC over stdio. Your tool server speaks a standard protocol.

// A minimal MCP server in JavaScript
const { Server } = require('@modelcontextprotocol/sdk/server/index.js');
const { StdioServerTransport } = require('@modelcontextprotocol/sdk/server/stdio.js');

class CalculatorServer {
  constructor() {
    this.server = new Server({
      name: 'calculator-server',
      version: '1.0.0'
    }, {
      capabilities: {
        tools: {}
      }
    });
    
    // Register our calculator tool
    this.server.setRequestHandler('tools/list', async () => ({
      tools: [{
        name: 'calculator',
        description: 'Evaluate mathematical expressions',
        inputSchema: {
          type: 'object',
          properties: {
            expression: {
              type: 'string',
              description: 'The mathematical expression to evaluate'
            }
          },
          required: ['expression']
        }
      }]
    }));
    
    // Handle tool calls
    this.server.setRequestHandler('tools/call', async (request) => {
      if (request.params.name === 'calculator') {
        const { expression } = request.params.arguments;
        try {
          const result = eval(expression);  // Don't use eval in production!
          return {
            content: [{
              type: 'text',
              text: `Result: ${result}`
            }]
          };
        } catch (error) {
          return {
            content: [{
              type: 'text',
              text: `Error: ${error.message}`
            }],
            isError: true
          };
        }
      }
    });
  }
  
  async run() {
    const transport = new StdioServerTransport();
    await this.server.connect(transport);
  }
}

// Start the server
const server = new CalculatorServer();
server.run().catch(console.error);

📚 Go Deeper

MCP Specification — The complete protocol documentation

MCP on GitHub — Reference implementations and examples

You Built a Tool System

You just built the foundation of every AI agent system:

  • Function calling — How AI calls tools
  • ReAct loops — How AI chains multiple tools
  • Skills — How to package related tools
  • MCP — How to make tools universal

This isn't a demo. This is how ChatGPT Code Interpreter works. How Cursor writes code. How every AI agent that "does things" actually does them.

The difference between a chatbot and an agent? Tools.

What You Can Build Now

With tools, your AI can control anything with an API.

🔧 DevOps Agent
Deploy apps, manage servers, run builds
📊 Data Agent
Query databases, analyze spreadsheets
📱 Social Agent
Post tweets, send emails, manage calendar
🏠 Home Agent
Control lights, check weather, order groceries

The question isn't what's possible. Everything with an API is possible.

The question is what you'll build first.

🧠 Final Recall

Test yourself. No peeking. These questions cover everything you just learned.

1. Why do large language models need tools to be truly useful?





2. What is the key component needed to define a tool for function calling?





3. In the ReAct loop, what do the four steps represent?





4. What's the difference between a tool and a skill?





5. What problem does the Model Context Protocol (MCP) solve?





← Previous: Fine-Tune Next: Vision Model →