Introduction & Goals

Welcome to your first lesson in developing Claude agents with tool integration! In this lesson, you'll learn the foundational skill of preparing function schemas that enable Claude to understand and request the use of your custom tools through a process called function calling.

By the end of this lesson, you'll understand how to write Ruby methods in functions.rb and create JSON schemas in schemas.json that describe these methods to Claude. You'll also learn how to wire everything together in main.rb using a tool registry that maps schema names to Ruby Method objects. These schemas are the bridge that allows Claude to understand what your methods do and how to call them, even though Claude never sees your actual Ruby code. This foundational step is essential before you can build a complete Claude agent system that can execute tools and use their results.

How Claude Uses Tools Through Function Calling

Function calling is the mechanism that allows Claude to use external tools and capabilities beyond text generation. Here is how the process works conceptually:

  1. You provide Claude with function schemas (JSON descriptions of your tools).
  2. Claude analyzes user requests and determines if any of your tools would be helpful.
  3. If Claude decides a tool is needed, it responds with a tool use request that includes the function name and specific parameters.
  4. Your system receives this tool use request, looks up the corresponding Ruby method in your tool registry, and executes it with the provided parameters.
  5. Your system sends the method result back to Claude.
  6. Claude incorporates this result into its response to the user or decides to use additional tools if needed.

The key insight is that Claude only sees the schemas (JSON descriptions), never your actual Ruby code. The must contain all the information needs to understand what each tool does and how to use it correctly. This separation means you can organize your methods however you like — relies entirely on the descriptions to make decisions about tool usage.

Writing Ruby Tool Methods

When creating tool methods for Claude agents, your Ruby methods serve two purposes: they contain the actual logic that will be executed, and they provide the foundation for creating accurate schemas. While Claude never sees these methods directly, writing them clearly helps you create better schema descriptions.

In your functions.rb file, you can define your tool methods:

You will notice # frozen_string_literal: true at the top of our Ruby files. This is a common Ruby pragma that prevents strings from being modified after they are created, improving performance and memory usage. While not strictly necessary for tool integrations, it is a best practice in modern Ruby development.

This method includes a clear comment that describes what it does. Ruby methods are concise and straightforward — you define parameters without type annotations, and the last expression is automatically returned. These methods should be simple and focused, performing one clear task that you can describe accurately in a schema.

Remember that Claude will only see the schema you create, not this Ruby code. The comments are for your benefit when translating the method into a schema that can understand.

Creating JSON Schemas for Single Functions

JSON schemas are the structured descriptions that tell Claude exactly what each tool does and how to use it. These schemas are the only information Claude receives about your tools, making them crucial for successful function calling.

Each schema contains several essential components that Claude uses to understand your tool:

  • The name field identifies the tool and should be descriptive to help Claude understand what the tool does and when to use it. While it doesn't necessarily need to match your Ruby method name, keeping them consistent helps maintain clarity in your code.
  • The description tells Claude what the tool does and when to use it.
  • The input_schema section describes the parameters using format, where each property represents a method parameter with its data type and description.
Building Multiple Tool Schemas

Real Claude agents typically have access to multiple tools, each described by its own schema. Let's create a second method and organize our methods in a dedicated file. In your functions.rb file, you can collect your tool methods:

Organizing your methods in a separate file keeps your code organized and makes it easy to require them when needed with require_relative "functions". Each method follows the same pattern with clear comments, providing consistency across your tool collection.

Organizing Multiple Schemas in JSON Format

Just as you organize your Ruby methods in functions.rb, you can organize your corresponding schemas in a schemas.json file. This creates a clean separation between your implementation and the descriptions that Claude will see:

When Claude receives this array of schemas, it can analyze user requests and select the most appropriate tool. For example, if a user asks "What's 5 plus 3?", Claude would choose sum_numbers. If they ask "What's 4 times 7?", Claude would choose multiply_numbers. The clear descriptions in each schema help make these decisions accurately.

Function-to-Schema Mapping

After organizing your methods in functions.rb and schemas in schemas.json, you need to load both and create a mapping that connects them. This creates two essential components: the schemas that you provide to Claude, and the mapping that you use to execute methods when Claude requests them.

In your main.rb file:

The tool_schemas variable contains the parsed JSON array that you'll provide to Claude — this is how Claude learns about your available tools and their capabilities. The tools Hash is your internal mapping that you use to execute the correct Ruby method when Claude requests a tool by name.

This mapping must be precise — the keys in your tools Hash must exactly match the fields in your . If there is a mismatch, your system will not be able to execute the method when requests it.

Verifying Your Tool Schemas

Before using your tool schemas with Claude, it is important to verify they are structured correctly and contain all necessary information. In your main.rb file, you can add:

This displays your tool definitions in a formatted way, allowing you to review each schema's structure:

Review this output to ensure your schemas have clear descriptions, correct parameter types, and complete required field lists. These details are crucial since Claude depends entirely on this schema information to use your tools correctly.

Testing Your Function Mapping

With your schemas verified and mapping in place, you can test that your system can successfully execute tools based on their names. In your main.rb file, add:

Running ruby main.rb produces:

This confirms your method mapping works correctly and your system can execute tools by name using the .call method on Method objects. When Claude requests tool usage through function calling, you'll receive the tool name and parameters from Claude, then use this mapping to look up and call the corresponding Ruby method yourself, before sending the result back to Claude.

Summary & Next Steps

You've now learned the essential process of preparing tools for Claude agents through function calling. This involves writing well-structured Ruby methods in functions.rb, creating detailed JSON schemas in schemas.json that describe these methods to Claude, and setting up a mapping system in main.rb using a Hash that connects schema names to Method objects.

Remember the key separation: Claude only sees the schemas and uses them to make tool selection decisions and provide parameters. Your Ruby methods and Hash-based registry handle the actual execution. The quality of your schemas directly impacts how effectively Claude can use your tools.

You've verified that your schemas are well-formed and that your local tool dispatch mechanism works correctly. This foundation — prepared schemas and a working tool registry — sets you up for integrating with the Anthropic API's tool-calling capabilities in future lessons.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal