> ## Documentation Index
> Fetch the complete documentation index at: https://docs.stackone.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Vercel AI SDK

> Build AI applications with Vercel AI SDK and StackOne tools

export const GitHubCode = ({url, lang, title, children}) => {
  const urlMatch = url?.match(/https:\/\/github\.com\/([^\/]+)\/([^\/]+)\/blob\/([^\/]+)\/(.+?)(?:#L(\d+)(?:-L(\d+))?)?$/);
  const owner = urlMatch?.[1];
  const repo = urlMatch?.[2];
  const branch = urlMatch?.[3];
  const filePath = urlMatch?.[4];
  const startLine = urlMatch?.[5];
  const endLine = urlMatch?.[6];
  const getDisplayTitle = () => {
    if (title) return title;
    if (!filePath) return 'source';
    const parts = filePath.split('/');
    if (parts.length >= 2) {
      return parts.slice(-2).join('/');
    }
    return parts[parts.length - 1];
  };
  const displayPath = filePath || 'source';
  const displayTitle = getDisplayTitle();
  const lineInfo = startLine ? endLine ? `#L${startLine}-L${endLine}` : `#L${startLine}` : '';
  const fullUrl = url + (lineInfo && !url.includes('#L') ? lineInfo : '');
  const encodedUrl = encodeURIComponent(fullUrl);
  const iframeSrc = `https://emgithub.com/iframe.html?target=${encodedUrl}&style=github&type=code&showBorder=on&showLineNumbers=on&showFileMeta=on`;
  return <div style={{
    marginTop: '1.5rem',
    marginBottom: '1.5rem',
    border: '1px solid #e5e7eb',
    borderRadius: '0.75rem',
    overflow: 'hidden',
    boxShadow: '0 1px 3px 0 rgb(0 0 0 / 0.1)'
  }}>
      <div style={{
    display: 'flex',
    justifyContent: 'space-between',
    alignItems: 'center',
    padding: '1rem 1.25rem',
    backgroundColor: '#f9fafb',
    borderBottom: '1px solid #e5e7eb'
  }}>
        <div style={{
    display: 'flex',
    alignItems: 'center',
    gap: '0.75rem'
  }}>
          <svg width="22" height="22" viewBox="0 0 24 24" fill="currentColor" style={{
    flexShrink: 0
  }}>
            <path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z" />
          </svg>
          <span style={{
    fontWeight: 600,
    fontSize: '0.9375rem'
  }}>{displayTitle}</span>
        </div>
        {url && <a href={url} target="_blank" rel="noopener noreferrer" style={{
    fontSize: '0.875rem',
    color: '#3b82f6',
    textDecoration: 'none',
    fontWeight: 500
  }}>
            View on GitHub →
          </a>}
      </div>
      <div style={{
    padding: '0.5rem',
    minHeight: '400px',
    backgroundColor: '#ffffff'
  }}>
        {children || <iframe src={iframeSrc} style={{
    width: '100%',
    height: '420px',
    border: 'none',
    borderRadius: '0.25rem'
  }} loading="lazy" />}
      </div>
    </div>;
};

StackOne tools work with [Vercel AI SDK](https://sdk.vercel.ai/) to build AI applications using React, Next.js, and other frameworks.

## Overview

* **Streaming responses** with real-time tool execution
* **React hooks** for building AI UIs
* **Multi-step tool calls** with automatic handling
* **Framework compatibility** with Next.js, React, Vue, and more

```typescript theme={null}
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
import { StackOneToolSet } from '@stackone/ai';

async function createAgentForAccount(accountId: string) {
  /**
   * Create agent with tools for a specific account.
   *
   * In production, accountId comes from:
   * - User/tenant context
   * - Authentication middleware
   * - Database lookup
   */
  const toolset = new StackOneToolSet();

  // Fetch tools dynamically for this account
  const tools = await toolset.fetchTools({
    accountIds: [accountId]
  });

  // Convert to Vercel AI SDK format
  const aiSdkTools = await tools.toAISDK();

  const { text } = await generateText({
    model: openai('gpt-5.4'),
    tools: aiSdkTools,
    prompt: 'Get employee details for id: c28xIQaWQ6MzM5MzczMDA2NzMzMzkwNzIwNA',
    maxSteps: 3,
  });

  return text;
}

// Use the agent
// Get account ID from your app's auth context or StackOne dashboard
const accountId = 'your-account-id';
const result = await createAgentForAccount(accountId);
```

## Dynamic Tool Loading

Load tools based on available integrations:

```typescript theme={null}
import { StackOneToolSet } from '@stackone/ai';

const toolset = new StackOneToolSet();

// Load all tools for specific accounts (most common)
const tools = await toolset.fetchTools({
  accountIds: ["account-123"]
});

// Optional: Filter by operation type for security
const tools = await toolset.fetchTools({
  accountIds: ["account-123"],
  actions: ["*_list_*", "*_get_*"]  // Only list and get operations (read-only)
});

// Convert to Vercel AI SDK format
const aiSdkTools = await tools.toAISDK();
```

## Streaming Responses

Build real-time AI applications with streaming:

```typescript theme={null}
import { streamText } from 'ai';
import { openai } from '@ai-sdk/openai';

async function streamingAgent(accountId: string, prompt: string) {
  const toolset = new StackOneToolSet();
  const tools = await toolset.fetchTools({ accountIds: [accountId] });
  const aiSdkTools = await tools.toAISDK();

  const result = await streamText({
    model: openai('gpt-5.4'),
    tools: aiSdkTools,
    prompt,
    maxSteps: 5,
  });

  // Stream to client
  for await (const chunk of result.textStream) {
    process.stdout.write(chunk);
  }
}
```

## Multi-Tenant Support

Handle multiple customer accounts:

```typescript theme={null}
async function getAgentForUser(userId: string) {
  // Get user's account IDs from your database
  const accountIds = await db.getUserAccountIds(userId);

  const toolset = new StackOneToolSet();
  const tools = await toolset.fetchTools({ accountIds });

  return await tools.toAISDK();
}

// Usage in API endpoint
app.post('/api/chat', async (req, res) => {
  const { message, userId } = req.body;
  const tools = await getAgentForUser(userId);

  const result = await generateText({
    model: openai('gpt-5.4'),
    tools,
    prompt: message,
  });

  res.json({ response: result.text });
});
```

## Best Practices

### Account ID from Context

```typescript theme={null}
// Get from user/tenant context
const accountId = req.user.stackoneAccountId;
const accountId = await getAccountForTenant(tenantId);
```

### Error Handling

```typescript theme={null}
import { StackOneError } from '@stackone/ai';

try {
  const result = await generateText({ model, tools, prompt });
} catch (error) {
  if (error instanceof StackOneError) {
    console.error('StackOne error:', error.message);
    // Handle gracefully
  }
}
```

### Tool Filtering

```typescript theme={null}
// Only expose read operations (optional - for security)
const tools = await toolset.fetchTools({
  accountIds: [accountId],
  actions: ["*_list_*", "*_get_*"]  // Only list and get
});

// Or exclude dangerous operations
const tools = await toolset.fetchTools({
  accountIds: [accountId],
  actions: ["!*_delete_*"]  // Everything except deletes
});
```

## Example

<GitHubCode url="https://github.com/StackOneHQ/stackone-ai-node/blob/main/examples/ai-sdk-integration.ts" lang="typescript">
  <iframe src="https://emgithub.com/iframe.html?target=https%3A%2F%2Fgithub.com%2FStackOneHQ%2Fstackone-ai-node%2Fblob%2Fmain%2Fexamples%2Fai-sdk-integration.ts&style=github&type=code&showBorder=on&showLineNumbers=on&showFileMeta=on" style={{ width: '100%', height: '400px', border: 'none' }} loading="lazy" />
</GitHubCode>

## Agent-Driven Search

Let the LLM discover tools on its own using `tool_search` + `tool_execute`:

```typescript theme={null}
const toolset = new StackOneToolSet({
  search: { method: 'semantic', topK: 3 },
  accountId: 'your-account-id',
});

const tools = toolset.getTools({ accountIds: ['your-account-id'] });

// LLM receives only 2 tools - it searches and executes autonomously
const { text } = await generateText({
  model: openai('gpt-5.4'),
  tools: await tools.toAISDK(),
  prompt: 'List my upcoming Calendly events for the next week.',
  stopWhen: stepCountIs(10),
});
```

See [Tool Search](/agents/typescript/tool-search#agent-driven-search) for more details.

## Next Steps

* [Basic Usage](/agents/typescript/basic-usage) for fetching and executing tools
* [Tool Search](/agents/typescript/tool-search) for search patterns
* [Tool Filtering](/agents/typescript/tool-filtering) for security
* [OpenAI Integration](/agents/typescript/frameworks/openai-integration) for alternative approach
