Skip to main content
When your Mastra agents are integrated with CometChat, they automatically receive rich contextual information about the user and message through the cometchatContext object. This enables your agents to provide personalized responses, authenticate API calls, and access session-specific data.

Runtime Context Structure

The cometchatContext is available within Mastra’s runtime context and contains the following structure:

Accessing Context in Mastra Agents

Runtime Context Access Pattern

In Mastra, you access the CometChat context through the runtimeContext.get() method within your tool’s execute function. The context data is stored under the key 'cometchatContext':

In Tool Functions

Access the context within your Mastra tool functions:

Common Use Cases

User Authentication & Business Logic

Use the context to validate user access based on sender information:

Role-Based Responses

Customize agent behavior based on user roles:

Session Management

Access session-specific data for stateful interactions:

Custom Validation Patterns

Implement sophisticated business logic validation using message metadata:

Next Steps