Skip to main content
StackOne tools integrate seamlessly with Vercel AI SDK 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
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 = tools.toAISDK();

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

  return text;
}

// Use the agent
const accountId = getCurrentUserAccount(); // Your function
const result = await createAgentForAccount(accountId);

Dynamic Tool Loading

Load tools based on available integrations:
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 = tools.toAISDK();

Streaming Responses

Build real-time AI applications with streaming:
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 = tools.toAISDK();

  const result = await streamText({
    model: openai('gpt-5'),
    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:
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 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'),
    tools,
    prompt: message,
  });

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

Best Practices

Account ID from Context

// Get from user/tenant context
const accountId = req.user.stackoneAccountId;
const accountId = await getAccountForTenant(tenantId);

Error Handling

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

// 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
});

Next Steps