mastra-ai/mastra · error

MCP server with id ${mcpServer.id} already exists

Error message

MCP server with id ${mcpServer.id} already exists

What it means

The in-memory MCP servers storage domain throws this from `create` when a server record with the given id already exists in `mcpServers`. It enforces id uniqueness and refuses to overwrite the existing server configuration.

Source

Thrown at packages/core/src/storage/domains/mcp-servers/inmemory.ts:49

  async dangerouslyClearAll(): Promise<void> {
    this.db.mcpServers.clear();
    this.db.mcpServerVersions.clear();
  }

  // ==========================================================================
  // MCP Server CRUD Methods
  // ==========================================================================

  async getById(id: string): Promise<StorageMCPServerType | null> {
    const config = this.db.mcpServers.get(id);
    return config ? this.deepCopyConfig(config) : null;
  }

  async create(input: { mcpServer: StorageCreateMCPServerInput }): Promise<StorageMCPServerType> {
    const { mcpServer } = input;

    if (this.db.mcpServers.has(mcpServer.id)) {
      throw new Error(`MCP server with id ${mcpServer.id} already exists`);
    }

    const now = new Date();
    const newConfig: StorageMCPServerType = {
      id: mcpServer.id,
      status: 'draft',
      activeVersionId: undefined,
      authorId: mcpServer.authorId,
      metadata: mcpServer.metadata,
      createdAt: now,
      updatedAt: now,
    };

    this.db.mcpServers.set(mcpServer.id, newConfig);

    // Extract config fields from the flat input (everything except record fields)
    const { id: _id, authorId: _authorId, metadata: _metadata, ...snapshotConfig } = mcpServer;

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Use a unique id generator for each new MCP server.
  2. Check existence first and update the existing record instead of creating.
  3. Reset the in-memory storage (new instance) before repeated seeding.

Example fix

// before
await mcpServers.create({ mcpServer: { id: 'my-server', ...cfg } });
// after
await mcpServers.create({ mcpServer: { id: crypto.randomUUID(), ...cfg } });
Defensive patterns

Strategy: validation

Validate before calling

const existing = await mcpServers.list({ perPage: false });
if (existing.results.some(s => s.id === mcpServer.id)) {
  throw new Error(`Refusing to create: MCP server ${mcpServer.id} already exists`);
}

Try / catch

try {
  await mcpServers.create({ mcpServer });
} catch (err) {
  if (err instanceof Error && /already exists/.test(err.message)) return; // idempotent seed
  throw err;
}

Prevention

When it happens

Trigger: Calling `storage.mcpServers.create({ mcpServer })` where `db.mcpServers.has(mcpServer.id)` is true — the id was already created or seeded (e.g., by seedAgent).

Common situations: Re-running seed scripts against non-cleared storage; static ids like 'my-server' reused across runs; retries re-invoking create after partial success.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/20e02168e9440a4c. Report an issue: GitHub.