Welcome to the final chapter of the TaskGetTool tutorial!
In the previous chapters, we built all the individual components of our tool:
Right now, these are just loose parts scattered on a workbench. In this chapter, we will assemble them into a single, functioning unit called the Tool Definition.
Imagine you are building a robot. You have the fingers, the motors, and the wires, but you need to attach them all to a single arm so the robot knows how to use them.
The Tool Definition is that arm. It is the bridge that connects your code to the AI Agent.
The Central Use Case: The AI system needs to interact with your code, but it cannot read your raw TypeScript files. It needs a standardized "ID Card" that tells it:
Without this definition, the AI doesn't know your tool exists.
We use a helper function called buildTool. This creates a unified object containing everything the AI needs to know.
Just like a person has a name and a job title, a tool needs a unique Name (for the system) and a Description (for the AI to understand its purpose).
We plug in the Input and Output schemas we built in Chapter 2. This tells the system how to validate data entering and leaving the tool.
We attach the actual executable code (the call function). This is the engine that runs when the AI decides to use the tool.
We set rules like isReadOnly. This tells the AI: "This tool is safe; it won't delete data or crash the server."
Let's look at TaskGetTool.ts and see how we use buildTool to wire everything together. We will break this large object into small, understandable pieces.
We start by calling buildTool. This function ensures we don't forget any required fields.
// Inside TaskGetTool.ts
export const TaskGetTool = buildTool({
name: TASK_GET_TOOL_NAME, // e.g. 'TaskGet'
userFacingName() {
return 'TaskGet' // Friendly name for UI logs
},
// ... other properties go here
})
Explanation:
export const: We export this so the main application can load it.name: A unique machine-readable ID.Next, we attach the instructions we wrote in Chapter 3: Prompt Configuration.
// Inside buildTool({ ... })
async description() {
return DESCRIPTION // "Get a task by ID..."
},
async prompt() {
return PROMPT // The detailed usage guide
},
Explanation: When the AI starts up, it calls these functions to "read" the manual. It learns what the tool does before it ever tries to use it.
We connect the Zod schemas from Chapter 2: Data Contract Schemas.
// Inside buildTool({ ... })
get inputSchema(): InputSchema {
return inputSchema() // Validates arguments (taskId)
},
get outputSchema(): OutputSchema {
return outputSchema() // Validates return data
},
Explanation:
By adding these getters, we ensure that every request is automatically checked. If the AI sends bad data, buildTool rejects it before it reaches our logic.
This is the heart of the tool. The call function performs the actual work.
// Inside buildTool({ ... })
async call({ taskId }) {
// 1. Get the data
const task = await getTask(getTaskListId(), taskId)
// 2. Return the structured object
// (We return the Task Domain Entity from Chapter 1)
if (!task) return { data: { task: null } }
return {
data: { task: { /* ... mapped fields ... */ } }
}
},
Explanation: This is where the code meets the database. Notice how simple it is? Because we handled validation in the Schema and formatting in the View layer, this function only needs to fetch and return data.
Finally, we add flags to help the system manage the tool.
// Inside buildTool({ ... })
shouldDefer: true, // Run in background if slow
isEnabled() {
return isTodoV2Enabled() // Feature flag check
},
isReadOnly() {
return true // Safe! No side effects.
},
Explanation:
isReadOnly(): Crucial for trust. The AI knows it can call this tool freely without breaking anything.isEnabled(): Allows us to turn the tool on or off based on system settings (feature flags).What happens when you run the application? How does this object turn into AI magic?
TaskGetTool.name, description, and inputSchema.taskId string."Think of the Tool Definition like publishing an App to the App Store.
If you don't fill out the App Store form (the buildTool object) correctly, the store (the AI) won't let users download or use your app.
Congratulations! You have completed the TaskGetTool tutorial.
In this final chapter, we learned that a Tool Definition is a configuration object that:
call function.
By wrapping our logic in buildTool, we created a safe, well-documented, and structured capability that an AI agent can use to help users get their work done.
You now have a fully functional tool that retrieves tasks, understands dependencies, and communicates clearly with an AI!
Generated by Code IQ