supermemory/packages/ai-sdk
MaheshtheDev f051af098e fix(mcp): bound tool inputs and scope get_document to the active space (#1593)
Cherry-picks #1582, #1583, #1584 and #1585 from @Sravanjangam (security audit #1578) onto one branch.

- MCP: `get_document` scopes to the active space like its sibling read tools, `fetch-graph-data` bounds page/limit, `guided-save` caps prefill at 200k, and `whoAmI` no longer returns the transport session id.
- ai-sdk: search limit clamped to 1-50 with a 30s client timeout.
- validation: caps on `DocumentsWithMemoriesQuerySchema.limit` and `BulkDeleteMemoriesSchema.containerTags`.
- Raycast: `metadata.url` is parsed and only http(s) is offered to the OS opener.

Dropped his `add_memory` permission gate: it checked the target against the list of existing spaces, so writes to a new space failed and the no-active-space path surfaced `No write access to space "undefined"`. Write permission stays enforced in the API via `containerTagGate`.

Hardening and consistency rather than a security fix, since the API already enforces every permission boundary here.

Co-Authored-By: Sravanjangam <163002695+Sravanjangam@users.noreply.github.com>
2026-08-24 21:36:04 +00:00
..
src fix(mcp): bound tool inputs and scope get_document to the active space (#1593) 2026-08-24 21:36:04 +00:00
.npmignore feat: add tsdown.config.ts (#379) 2025-08-23 17:40:04 +00:00
package.json fix: username issue and bun lock issue (#818) 2026-03-31 21:41:25 +00:00
README.md fix: model names 2025-10-03 02:41:49 -07:00
tsconfig.json feat: add tsdown.config.ts (#379) 2025-08-23 17:40:04 +00:00
tsdown.config.ts feat: add tsdown.config.ts (#379) 2025-08-23 17:40:04 +00:00

supermemory AI SDK Utilities

Vercel AI SDK utilities for supermemory

Installation

npm install @supermemory/ai-sdk
# or
bun add @supermemory/ai-sdk
# or
pnpm add @supermemory/ai-sdk
# or
yarn add @supermemory/ai-sdk

Features

Choose one of the following approaches (they cannot be used together):

  • Infinite Chat Provider: Connect to various LLM providers with unlimited context support
  • Memory Tools: Search, add, and fetch memories from supermemory using AI agents

Infinite Chat Provider

The infinite chat provider allows you to connect to various LLM providers with supermemory's context management.

import { generateText } from 'ai'

// Using a custom provider URL
const supermemoryOpenai = createOpenAI({
  baseUrl: 'https://api.supermemory.ai/v3/https://api.openai.com/v1',
  apiKey: 'your-provider-api-key',
  headers: {
    'x-supermemory-api-key': 'supermemory-api-key',
    'x-sm-conversation-id': 'conversation-id'
  }
})

const result = await generateText({
  model: supermemoryOpenai('gpt-5'),
  messages: [
    { role: 'user', content: 'Hello, how are you?' }
  ]
})

Complete Infinite Chat Example

import { generateText } from 'ai'

const supermemoryApiKey = process.env.SUPERMEMORY_API_KEY!
const openaiApiKey = process.env.OPENAI_API_KEY!

// Initialize infinite chat provider
const supermemoryOpenai = createOpenAI({
  baseUrl: 'https://api.supermemory.ai/v3/https://api.openai.com/v1',
  apiKey: 'your-provider-api-key',
  headers: {
    'x-supermemory-api-key': 'supermemory-api-key',
    'x-sm-conversation-id': 'conversation-id'
  }
})

async function chat(userMessage: string) {
  const result = await generateText({
    model: supermemoryOpenai('gpt-5'),
    messages: [
      {
        role: 'system',
        content: 'You are a helpful assistant with unlimited context.'
      },
      {
        role: 'user',
        content: userMessage
      }
    ]
    // No tools - infinite chat handles context automatically
  })

  return result.text
}

Configuration

// Option 1: Use a named provider
interface ConfigWithProviderName {
  providerName: 'openai' | 'anthropic' | 'openrouter' | 'deepinfra' | 'groq' | 'google' | 'cloudflare'
  providerApiKey: string
  headers?: Record<string, string>
}

// Option 2: Use a custom provider URL
interface ConfigWithProviderUrl {
  providerUrl: string
  providerApiKey: string
  headers?: Record<string, string>
}

Memory Tools

supermemory tools allow AI agents to interact with user memories for enhanced context and personalization.

import { supermemoryTools } from '@supermemory/ai-sdk'
import { generateText } from 'ai'

const result = await generateText({
  model: openai('gpt-5'),
  messages: [
    { role: 'user', content: 'What do you remember about my preferences?' }
  ],
  tools: {
    ...supermemoryTools('your-supermemory-api-key', {
  // Optional: specify a base URL for self-hosted instances
  baseUrl: 'https://api.supermemory.com',

  // Use either projectId OR containerTags, not both
  projectId: 'your-project-id',
  // OR
  containerTags: ['tag1', 'tag2']
}),
// Your other tools go here
  }
})

Complete Memory Tools Example

import { supermemoryTools } from '@supermemory/ai-sdk'
import { generateText } from 'ai'
import { openai } from '@ai-sdk/openai'

const supermemoryApiKey = process.env.SUPERMEMORY_API_KEY!

async function chatWithTools(userMessage: string) {
  const result = await generateText({
    model: openai('gpt-5'), // Use standard provider
    messages: [
      {
        role: 'system',
        content: 'You are a helpful assistant with access to user memories.'
      },
      {
        role: 'user',
        content: userMessage
      }
    ],
    tools: {
      ...supermemoryTools(supermemoryApiKey, {
        containerTags: ['my-user-id']
      })
    },
    maxToolRoundtrips: 5
  })

  return result.text
}

Configuration

interface SupermemoryConfig {
  // Optional: Base URL for API calls (default: https://api.supermemory.com)
  baseUrl?: string

  // Container tags for organizing memories (cannot be used with projectId)
  containerTags?: string[]

  // Project ID for scoping memories (cannot be used with containerTags)
  projectId?: string
}

Self-Hosted supermemory

If you're running a self-hosted supermemory instance:

const tools = supermemoryTools('your-api-key', {
  baseUrl: 'https://your-supermemory-instance.com',
  containerTags: ['production', 'user-memories']
})

Available Tools

Search Memories

Search through user memories using semantic matching.

const searchResult = await tools.searchMemories.execute({
  informationToGet: 'user preferences about coffee'
})
Add Memory

Add new memories to the user's memory store.

const addResult = await tools.addMemory.execute({
  memory: 'User prefers dark roast coffee in the morning'
})
Fetch Memory

Retrieve a specific memory by its ID.

const fetchResult = await tools.fetchMemory.execute({
  memoryId: 'memory-id-123'
})

Using Individual Tools

For more flexibility, you can import and use individual tools:

import {
  searchMemoriesTool,
  addMemoryTool,
  fetchMemoryTool
} from '@supermemory/ai-sdk'

const searchTool = searchMemoriesTool('your-api-key', {
  projectId: 'your-project-id'
})

// Use only the search tool
const result = await generateText({
  model: openai('gpt-5'),
  messages: [...],
  tools: {
    searchMemories: searchTool
  }
})

Error Handling

All tool executions return a result object with a success field:

const result = await tools.searchMemories.execute({
  informationToGet: 'user preferences'
})

if (result.success) {
  console.log('Found memories:', result.results)
  console.log('Total count:', result.count)
} else {
  console.error('Error searching memories:', result.error)
}

Development

Running Tests

# Run all tests
bun test

# Run tests in watch mode
bun test --watch

Environment Variables for Tests

All tests require API keys to run. Copy .env.example to .env and set the required values:

cp .env.example .env

Required:

  • SUPERMEMORY_API_KEY: Your Supermemory API key
  • PROVIDER_API_KEY: Your AI provider API key (OpenAI, Anthropic, etc.)
  • OPENAI_API_KEY: Your OpenAI API key for tool integration tests

Optional:

  • SUPERMEMORY_BASE_URL: Custom Supermemory base URL (defaults to https://api.supermemory.ai)
  • PROVIDER_NAME: Provider name (defaults to openai) - one of: openai, anthropic, openrouter, deepinfra, groq, google, cloudflare
  • PROVIDER_URL: Custom provider URL (use instead of PROVIDER_NAME)
  • MODEL_NAME: Model to use in tests (defaults to gpt-3.5-turbo)

Tests will fail if required API keys are not provided.

License

MIT

Support

Email our 24/7 Founder/CEO/Support Executive