# AI SDK 6 Tool Execution: Override & Extend Patterns
> Comprehensive guide for customizing tool execution in TPMJS with Vercel AI SDK 6
## Table of Contents
1. [Overview](#overview)
2. [AI SDK 6 Tool Architecture](#ai-sdk-6-tool-architecture)
3. [How TPMJS Tool Execution Works](#how-tpmjs-tool-execution-works)
4. [Override Patterns](#override-patterns)
5. [Extension Patterns](#extension-patterns)
6. [Middleware Approach](#middleware-approach)
7. [Factory Pattern Implementation](#factory-pattern-implementation)
8. [Complete Examples](#complete-examples)
9. [Best Practices](#best-practices)
10. [API Reference](#api-reference)
---
## Overview
When using tools from the TPMJS registry, developers have different needs:
| Use Case | Approach |
|----------|----------|
| **Use default execution** | Import and use `registryExecuteTool` directly |
| **Modify parameters** | Wrap the tool with custom logic |
| **Add logging/telemetry** | Use middleware pattern |
| **Replace execution entirely** | Create custom tool with same schema |
| **Extend with pre/post processing** | Use factory functions |
| **Add approval workflows** | Use AI SDK 6's `needsApproval` feature |
This document covers all patterns with practical examples.
---
## AI SDK 6 Tool Architecture
### The `tool()` Function
The AI SDK's `tool()` helper creates typed tool definitions:
```typescript
import { tool } from 'ai';
import { z } from 'zod';
const myTool = tool({
description: 'What the tool does',
inputSchema: z.object({
param1: z.string().describe('Parameter description'),
}),
execute: async ({ param1 }, options) => {
// options includes:
// - toolCallId: unique identifier for this call
// - messages: conversation history
// - abortSignal: for cancellation
// - experimental_context: custom data from generateText/streamText
return { result: 'value' };
},
});
```
### Execute Function Signature
```typescript
type ExecuteFunction = (
input: INPUT,
options: {
toolCallId: string;
messages: CoreMessage[];
abortSignal: AbortSignal;
experimental_context?: unknown;
}
) => Promise