mastra-ai/mastra · error · Error

Cannot register an elicitation handler after connecting unle

Error message

Cannot register an elicitation handler after connecting unless elicitation capability was configured before initialization.

What it means

Elicitation capability must be negotiated during MCP initialization. registerElicitationHandler tries to late-register the elicitation capability on the connected Client via registerCapabilities({ elicitation: { form: {} } }); if the underlying SDK throws (capability cannot be added after initialization), this error is thrown with the original error as cause.

Source

Thrown at packages/mcp/src/client/client.ts:1219

    this.log('debug', 'Setting resource list changed notification handler');
    this.client.setNotificationHandler('notifications/resources/list_changed', () => {
      handler();
    });
  }

  setElicitationRequestHandler(handler: ElicitationHandler): void {
    this.log('debug', 'Setting elicitation request handler');
    // The handler serves both protocol eras: on legacy (2025-era) connections it
    // answers elicitation/create wire requests; on negotiated 2026-07-28
    // connections the SDK's multi-round-trip driver dispatches embedded
    // elicitation requests from input_required results through the same
    // registered handler and retries the originating call automatically.
    if (!this.hasElicitationCapability) {
      try {
        this.client.registerCapabilities({ elicitation: { form: {} } });
        this.hasElicitationCapability = true;
      } catch (error) {
        throw new Error(
          'Cannot register an elicitation handler after connecting unless elicitation capability was configured before initialization.',
          { cause: error },
        );
      }
    }

    this.client.setRequestHandler('elicitation/create', async request => {
      this.log('debug', `Received elicitation request: ${request.params.message}`);
      return handler(request.params);
    });
  }

  setProgressNotificationHandler(handler: ProgressHandler): void {
    this.log('debug', 'Setting progress notification handler');
    this.client.setNotificationHandler('notifications/progress', notification => {
      handler(notification.params);
    });
  }

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Configure the elicitation capability up front when creating the client, before connect()/initialization
  2. Restart/reconnect the client with elicitation capability enabled if the handler is added late
  3. Verify the MCP SDK/server version supports elicitation and form capability
  4. Check the `cause` of the error for the underlying SDK failure

Example fix

// before
const client = new InternalMastraMCPClient({ name: 'x', url });
await client.connect();
client.registerElicitationHandler(handler); // throws
// after
const client = new InternalMastraMCPClient({ name: 'x', url, capabilities: { elicitation: { form: {} } } });
client.registerElicitationHandler(handler);
await client.connect();
Defensive patterns

Strategy: validation

Validate before calling

if (!client.isConnected()) {
  // safe: capability can still be configured before initialization
  client.registerElicitationHandler(handler);
} else {
  throw new Error('Register elicitation handlers before connect()');
}

Try / catch

try {
  client.registerElicitationHandler(handler);
} catch (e) {
  if (e instanceof Error && e.message.includes('elicitation capability')) {
    // recreate the client with elicitation capability configured pre-init, then reconnect
  }
  throw e;
}

Prevention

When it happens

Trigger: Setting an elicitation handler after connect() has completed initialization on a server/client pair that did NOT negotiate the elicitation capability during initialize; the late registerCapabilities call fails and is rethrown wrapped.

Common situations: Adding elicitation support to an existing client without restarting the connection; server not advertising elicitation support; constructing the client without the elicitation capability option and then wiring the handler post-connect.

Related errors


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