chatkit-botbuilder
Guide for creating production-grade ChatKit chatbots that integrate OpenAI Agents SDK with MCP tools and custom backends. Use when building AI-powered chatbots with specialized capabilities, real-time task execution, and user isolation for any application.
What this skill does
# ChatKit Botbuilder
## Overview
Create production-grade chatbots using the OpenAI ChatKit framework. This skill enables building chatbots that:
- **Integrate AI Agents**: Use OpenAI Agents SDK for intelligent conversation handling
- **Execute Tools**: Connect MCP (Model Context Protocol) tools for real-world task execution
- **Support Custom Backends**: Build FastAPI backends with full protocol support
- **Ensure User Isolation**: Implement multi-user systems with JWT authentication
- **Real-Time Synchronization**: Enable live UI updates when chatbot performs actions
- **Flexible Deployment**: Deploy to web, mobile, or desktop applications
This skill provides the complete architecture pattern for ChatKit integration, from frontend configuration to backend server implementation.
---
## When to Use This Skill
Use this skill when you need to:
1. **Build a task management chatbot** - Create conversational interfaces for task creation, updates, completion
2. **Integrate AI into existing apps** - Add ChatKit to dashboards, web apps, or platforms
3. **Create specialized AI assistants** - Build domain-specific chatbots with custom tool integrations
4. **Implement multi-user chatbots** - Create systems where each user has isolated conversations and data
5. **Add real-time capabilities** - Build chatbots that trigger actual application changes
6. **Deploy AI conversations** - Create chatbots that interact with your database and APIs
---
## Architecture Overview
### High-Level Flow
```
User Message
↓
ChatKit Frontend (React/Next.js)
↓ [JWT Token in Authorization Header]
↓
FastAPI Backend (ChatKit Server)
↓ [Extract user_id from JWT]
↓
OpenAI Agent (Agents SDK)
↓ [Needs tool execution]
↓
MCP Tools (Custom Tool Functions)
↓ [Creates/Updates/Lists data]
↓
Database (User-Isolated Data)
↓
Response → ChatKit → Frontend → User
```
### Key Components
1. **Frontend (Next.js + ChatKit SDK)**
- ChatKit UI component with conversation history
- JWT token management in localStorage
- Custom fetch wrapper with Bearer token authentication
- Real-time auto-refresh to sync with backend changes
2. **Backend (FastAPI + ChatKit Server)**
- ChatKit protocol endpoint handling requests
- MyChatKitServer class extending ChatKitServer
- User isolation through JWT middleware
- Tool wrapper functions for automatic user_id injection
3. **Agent (OpenAI Agents SDK)**
- Task management agent with instructions
- Tool registration and execution
- Session management
4. **Tools (MCP + Custom Functions)**
- Wrapped functions injecting user_id automatically
- Database operations with user isolation
- Consistent error handling
5. **Database**
- SQLModel ORM models
- Per-user task filtering
- Conversation persistence
---
## Quick Start Workflow
### Phase 1: Backend Setup (FastAPI)
**1. Create ChatKit Server Class**
```python
from chatkit.server import ChatKitServer
from chatkit.store import Store
class MyChatKitServer(ChatKitServer):
def __init__(self):
store = CustomChatKitStore()
super().__init__(store=store)
async def respond(self, thread, input, context):
"""Process user message and stream AI response"""
user_id = getattr(context, 'user_id', None)
# Create agent with wrapped tools
# Stream response using official pattern
```
**2. Create MCP Tool Wrappers**
```python
# Extract user_id from context and inject into tool calls
def add_task_wrapper(title: str, description: str = None):
return mcp_add_task(user_id=user_id, title=title, description=description)
def list_tasks_wrapper(status: str = "all"):
return mcp_list_tasks(user_id=user_id, status=status)
```
**3. Create FastAPI Endpoint**
```python
@router.post("/api/v1/chatkit")
async def chatkit_protocol_endpoint(request: Request):
user_id = request.state.user_id # From JWT middleware
context = create_context_object(user_id=user_id)
result = await chatkit_server.process(body, context)
return StreamingResponse(result, media_type="text/event-stream")
```
**4. Configure JWT Middleware**
```python
class JWTAuthMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request, call_next):
# Extract JWT token from Authorization header
# Decode and set request.state.user_id
# All endpoints have access to authenticated user_id
```
### Phase 2: Frontend Setup (Next.js + React)
**1. Configure ChatKit SDK**
```typescript
const chatKitConfig: UseChatKitOptions = {
api: {
url: `${API_BASE_URL}/api/v1/chatkit`,
domainKey: 'your-domain-key',
fetch: authenticatedFetch, // Custom fetch with JWT
},
theme: 'light',
header: { enabled: true, title: { text: 'AI Chat' } },
history: { enabled: true },
}
```
**2. Create Authenticated Fetch Wrapper**
```typescript
async function authenticatedFetch(input, options) {
const token = localStorage.getItem('access_token')
const headers = {
...options?.headers,
'Authorization': `Bearer ${token}`,
}
return fetch(input, { ...options, headers })
}
```
**3. Integrate ChatKit Widget**
```typescript
import { ChatKitWidget } from '@openai/chatkit-react'
export default function Dashboard() {
return (
<div className="flex gap-4">
{/* Your app content */}
{showChat && (
<ChatKitWidget {...chatKitConfig} />
)}
</div>
)
}
```
**4. Add Auto-Refresh for Real-Time Sync**
```typescript
useEffect(() => {
if (!showChatKit) return
// Refresh immediately when chat opens
fetchTasks()
// Refresh every 1 second for real-time updates
const interval = setInterval(() => {
fetchTasks()
}, 1000)
return () => clearInterval(interval)
}, [showChatKit])
```
### Phase 3: Tool Implementation (MCP)
**1. Create MCP Tools with User Isolation**
```python
def add_task(user_id: str, title: str, description: Optional[str] = None):
"""Create task - receives user_id from wrapper"""
task = Task(
id=str(uuid.uuid4()),
user_id=user_id, # Critical: ensure user isolation
title=title,
description=description,
completed=False,
created_at=datetime.utcnow(),
)
with Session(engine) as session:
session.add(task)
session.commit()
```
**2. Register MCP Tools**
```python
mcp_server = MCPServer()
mcp_server.register_tool("add_task", add_task)
mcp_server.register_tool("list_tasks", list_tasks)
mcp_server.register_tool("delete_task", delete_task)
# ... more tools
```
---
## Core Patterns & Best Practices
### 1. User Isolation Strategy
**Three-Level Guarantee:**
1. **Middleware Level** - JWT validation ensures only authenticated users
2. **Tool Level** - Wrapper functions automatically inject user_id
3. **Database Level** - All queries filtered by user_id
```python
# Middleware extracts user_id from token
request.state.user_id = payload.get("user_id")
# Tool wrapper captures and injects it
def add_task_wrapper(title):
return mcp_add_task(user_id=user_id, ...)
# Database enforces it
WHERE user_id = ? AND task_id = ?
```
### 2. Message Flow with User Context
```
User sends: "Create a task called 'Buy milk'"
↓
ChatKit Protocol: POST /api/v1/chatkit
Header: Authorization: Bearer <JWT>
Body: { "type": "message", "text": "Create..." }
↓
JWT Middleware:
Extracts user_id from token → request.state.user_id
↓
ChatKit Server (MyChatKitServer.respond):
Gets user_id from context
Creates wrapper functions capturing user_id
Passes wrappers to Agent
↓
OpenAI Agent:
Receives message: "Create a task..."
Selects tool: add_task_wrapper
Calls: add_task_wrapper(title="Buy milk")
↓
Wrapper Function:
Calls: mcp_add_task(user_id="user-123", title="Buy milk")
↓
MCP Tool:
Creates task with correct user_id
Returns: {"task_id": "...", "title": "Buy milk"}
↓
Agent Response:
"I've created 'Buy milk' task ✓"
Related in Backend & APIs
jfrog
IncludedInteract with the JFrog Platform via the JFrog CLI and REST/GraphQL APIs. Use this skill when the user wants to manage Artifactory repositories, upload or download artifacts, manage builds, configure permissions, manage users and groups, work with access tokens, configure JFrog CLI servers, search artifacts, manage properties, set up replication, manage JFrog Projects, run security audits or scans, look up CVE details, query exposures scan results from JFrog Advanced Security, manage release bundles and lifecycle operations, aggregate or export platform data, or perform any JFrog Platform administration task. Also use when the user mentions jf, jfrog, artifactory, xray, distribution, evidence, apptrust, onemodel, graphql, workers, mission control, curation, advanced security, exposures, or any JFrog product name.
cupynumeric-migration-readiness
IncludedPre-migration readiness assessor for porting NumPy to cuPyNumeric. Use BEFORE substantial porting work begins when the user asks whether code will scale on GPU, whether they should migrate to cuPyNumeric, which NumPy patterns transfer cleanly, what must be refactored before porting, or mentions pre-port assessment, scaling analysis, or refactor planning. Inspect the user's source code, look up NumPy usage, cross-reference the cuPyNumeric API support manifest, and distinguish distributed-scaling-friendly patterns from blockers such as unsupported APIs, scalar synchronization, host round-trips, Python/object-heavy control flow, shape/data-dependent branching, and in-place mutation hazards. Produce a verdict of READY, LIGHT REFACTOR, SIGNIFICANT REFACTOR, or NOT RECOMMENDED, with concrete refactor pointers.
alibabacloud-data-agent-skill
IncludedInvoke Alibaba Cloud Apsara Data Agent for Analytics via CLI to perform natural language-driven data analysis on enterprise databases. Data Agent for Analytics is an intelligent data analysis agent developed by Alibaba Cloud Database team for enterprise users. It automatically completes requirement analysis, data understanding, analysis insights, and report generation based on natural language descriptions. This tool supports: discovering data resources (instances/databases/tables) managed in DMS, initiating query or deep analysis sessions, real-time progress tracking, and retrieving analysis conclusions and generated reports. Use this Skill when users need to query databases, analyze data trends, generate data reports, ask questions in natural language, or mention "Data Agent", "data analysis", "database query", "SQL analysis", "data insights".
token-optimizer
IncludedReduce OpenClaw token usage and API costs through smart model routing, heartbeat optimization, budget tracking, and native 2026.2.15 features (session pruning, bootstrap size limits, cache TTL alignment). Use when token costs are high, API rate limits are being hit, or hosting multiple agents at scale. The 4 executable scripts (context_optimizer, model_router, heartbeat_optimizer, token_tracker) are local-only — no network requests, no subprocess calls, no system modifications. Reference files (PROVIDERS.md, config-patches.json) document optional multi-provider strategies that require external API keys and network access if you choose to use them. See SECURITY.md for full breakdown.
resend-cli
IncludedUse this skill when the task is specifically about operating Resend from an AI agent, terminal session, or CI job via the official resend CLI: installing/authenticating the CLI, sending/listing/updating/cancelling emails, batch sends, domains and DNS, webhooks and local listeners, inbound receiving, contacts, topics, segments, broadcasts, templates, API keys, profiles, or debugging Resend CLI/API failures. Trigger on mentions of Resend CLI, `resend`, `resend doctor`, `resend emails send`, `resend domains`, `resend webhooks listen`, `resend emails receiving`, or agent-friendly terminal automation.
alibabacloud-odps-maxframe-coding
IncludedUse this skill for MaxFrame SDK development and documentation navigation on Alibaba Cloud MaxCompute (ODPS). Helps answer MaxFrame API, concept, official example, and supported pandas API questions; create data processing programs; read/write MaxCompute tables; debug jobs (remote or local); and build custom DPE runtime images. Trigger when users mention MaxFrame, MaxCompute with MaxFrame, ODPS table processing, DPE runtime, MaxFrame docs/examples, DataFrame/Tensor operations, or GPU runtime setup. Works for both English and Chinese queries about Alibaba Cloud data processing with MaxFrame.