CLONESAHIBINDEN
FRONTEND GUIDE FOR AI CODING AGENTS - PART 5 - MCP BFF Integration
This document is a part of a REST API guide for the clonesahibinden project. It is designed for AI agents that will generate frontend code to consume the project’s backend.
This document provides comprehensive instructions for integrating the MCP BFF (Model Context Protocol - Backend for Frontend) service into the frontend application. The MCP BFF is the central gateway between the frontend AI chat and all backend services.
MCP BFF Architecture Overview
The Clonesahibinden application uses an MCP BFF service that aggregates multiple backend MCP servers into a single frontend-facing API. Instead of the frontend connecting to each service’s MCP endpoint directly, it communicates exclusively through the MCP BFF.
┌────────────┐ ┌───────────┐ ┌─────────────────┐
│ Frontend │────▶│ MCP BFF │────▶│ Auth Service │
│ (Chat UI) │ │ :3005 │────▶│ Business Svc 1 │
│ │◀────│ │────▶│ Business Svc N │
└────────────┘ SSE └───────────┘ └─────────────────┘
Key Responsibilities
- Tool Aggregation: Discovers and registers tools from all connected MCP services
-
Session Forwarding: Injects the user’s
accessTokeninto every MCP tool call - AI Orchestration: Routes user messages to the AI model, which decides which tools to call
- SSE Streaming: Streams chat responses, tool executions, and results to the frontend in real-time
- Elasticsearch: Provides direct search/aggregation endpoints across all project indices
- Logging: Provides log viewing and real-time console streaming endpoints
MCP BFF Service URLs
For the MCP BFF service, the base URLs are:
-
Preview:
https://clonesahibinden.prw.mindbricks.com/mcpbff-api -
Staging:
https://clonesahibinden-stage.mindbricks.co/mcpbff-api -
Production:
https://clonesahibinden.mindbricks.co/mcpbff-api
All endpoints below are relative to the MCP BFF base URL.
Authentication
All MCP BFF endpoints require authentication. The user’s access token (obtained from the Auth service login) must be included in every request:
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${accessToken}`,
};
Chat API (AI Interaction)
The chat API is the primary interface for AI-powered conversations. It supports both regular HTTP responses and SSE streaming for real-time output.
POST /api/chat — Regular Chat
Send a message and receive the complete AI response.
const response = await fetch(`${mcpBffUrl}/api/chat`, {
method: 'POST',
headers,
body: JSON.stringify({
message: "Show me all orders from last week",
conversationId: "optional-conversation-id", // for conversation context
context: {} // additional context
}),
});
POST /api/chat/stream — SSE Streaming Chat (Recommended)
Stream the AI response in real-time. This is the recommended approach for chat UIs as it provides immediate feedback.
const response = await fetch(`${mcpBffUrl}/api/chat/stream`, {
method: 'POST',
headers,
body: JSON.stringify({
message: "Create a new product called Widget",
conversationId: conversationId,
}),
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value, { stream: true });
const lines = chunk.split('\n');
for (const line of lines) {
if (line.startsWith('event: ')) {
const eventType = line.slice(7).trim();
// Handle event type
}
if (line.startsWith('data: ')) {
const data = JSON.parse(line.slice(6));
// Handle event data
}
}
}
SSE Event Types
The streaming endpoint emits the following event types:
| Event | Description | Data |
|---|---|---|
start |
Stream started | { conversationId } |
text |
AI text chunk | { text: "partial response..." } |
tool_start |
AI is calling a tool | { toolName, toolArgs } |
tool_executing |
Tool is being executed | { toolName } |
tool_result |
Tool execution completed |
{ toolName, result } —
check for __frontendAction
|
error |
Error occurred | { error: "message" } |
done |
Stream completed | { conversationId, fullResponse } |
Handling __frontendAction in Tool Results
When the AI calls certain tools (e.g., payment, secret reveal), the
tool result may contain a __frontendAction object. This
signals the frontend to render a special UI component instead of
displaying raw tool output.
// In your SSE handler for 'tool_result' events:
function handleToolResult(data) {
const action = extractFrontendAction(data.result);
if (action) {
// Render ActionCard component with this action
renderActionCard(action);
} else {
// Display raw tool result as JSON or formatted text
displayToolResult(data);
}
}
// Extract __frontendAction from various response formats
function extractFrontendAction(result) {
if (!result) return null;
if (result.__frontendAction) return result.__frontendAction;
// Unwrap MCP wrapper format
let data = result;
if (result?.result?.content) data = result.result;
if (data?.content && Array.isArray(data.content)) {
const textContent = data.content.find(c => c.type === 'text');
if (textContent?.text) {
try {
const parsed = JSON.parse(textContent.text);
if (parsed?.__frontendAction) return parsed.__frontendAction;
} catch { /* not JSON */ }
}
}
return null;
}
Frontend Action Types
| Action Type | Component | Description |
|---|---|---|
qrcode |
QrCodeActionCard |
Renders any string value as a QR code card |
dataView |
DataViewActionCard |
Fetches a Business API route and renders a grid or gallery |
payment |
PaymentActionCard |
“Pay Now” button that opens Stripe checkout modal |
QR Code Action (type: "qrcode")
Triggered by the showQrCode MCP tool. Renders a QR code
card from any string value.
{
"__frontendAction": {
"type": "qrcode",
"value": "https://example.com/invite/ABC123",
"title": "Invite Link",
"subtitle": "Scan to open"
}
}
Data View Action (type: "dataView")
Triggered by showBusinessApiListInFrontEnd or
showBusinessApiGalleryInFrontEnd. Frontend calls the
provided Business API route using the user’s bearer token, then
renders:
-
viewType: "grid"as tabular rows/columns -
viewType: "gallery"as image-first cards
{
"__frontendAction": {
"type": "dataView",
"viewType": "grid",
"title": "Recent Orders",
"serviceName": "commerce",
"apiName": "fetchListOrder",
"routePath": "/v1/_fetchlistorder",
"httpMethod": "GET",
"queryParams": { "pageNo": 1, "pageRowCount": 10 },
"columns": [
{ "field": "id", "label": "Order ID" },
{ "field": "orderAmount", "label": "Amount", "format": "currency" }
]
}
}
Payment Action (type: "payment")
Triggered by the initiatePayment MCP tool. Renders a
payment card with amount and a “Pay Now” button.
{
"__frontendAction": {
"type": "payment",
"orderId": "uuid",
"orderType": "order",
"serviceName": "commerce",
"amount": 99.99,
"currency": "USD",
"description": "Order #abc123"
}
}
Conversation Management
// List user's conversations
GET /api/chat/conversations
// Get conversation history
GET /api/chat/conversations/:conversationId
// Delete a conversation
DELETE /api/chat/conversations/:conversationId
MCP Tool Discovery & Direct Invocation
The MCP BFF exposes endpoints for discovering and directly calling MCP tools (useful for debugging or building custom UIs).
GET /api/tools — List All Tools
const response = await fetch(`${mcpBffUrl}/api/tools`, { headers });
const { tools, count } = await response.json();
// tools: [{ name, description, inputSchema, service }, ...]
GET /api/tools/service/:serviceName — List Service Tools
const response = await fetch(`${mcpBffUrl}/api/tools/service/commerce`, { headers });
const { tools } = await response.json();
POST /api/tools/call — Call a Tool Directly
const response = await fetch(`${mcpBffUrl}/api/tools/call`, {
method: 'POST',
headers,
body: JSON.stringify({
toolName: "listProducts",
args: { page: 1, limit: 10 },
}),
});
const result = await response.json();
GET /api/tools/status — Connection Status
const status = await fetch(`${mcpBffUrl}/api/tools/status`, { headers });
// Returns health of each MCP service connection
POST /api/tools/refresh — Reconnect Services
await fetch(`${mcpBffUrl}/api/tools/refresh`, { method: 'POST', headers });
// Reconnects to all MCP services and refreshes the tool registry
Elasticsearch API
The MCP BFF provides direct access to Elasticsearch for searching, filtering, and aggregating data across all project indices.
All Elasticsearch endpoints are under /api/elastic.
GET /api/elastic/allIndices — List Project Indices
Returns all Elasticsearch indices belonging to this project (prefixed
with clonesahibinden_).
const indices = await fetch(`${mcpBffUrl}/api/elastic/allIndices`, { headers });
// ["clonesahibinden_products", "clonesahibinden_orders", ...]
POST /api/elastic/:indexName/rawsearch — Raw Elasticsearch Query
Execute a raw Elasticsearch query on a specific index.
const response = await fetch(`${mcpBffUrl}/api/elastic/products/rawsearch`, {
method: 'POST',
headers,
body: JSON.stringify({
query: {
bool: {
must: [
{ match: { status: "active" } },
{ range: { price: { gte: 10, lte: 100 } } }
]
}
},
size: 20,
from: 0,
sort: [{ createdAt: "desc" }]
}),
});
const { total, hits, aggregations, took } = await response.json();
// hits: [{ _id, _index, _score, _source: { ...document... } }, ...]
Note: The index name is automatically prefixed with
clonesahibinden_ if not already prefixed.
POST /api/elastic/:indexName/search — Simplified Search
A higher-level search API with built-in support for filters, sorting, and pagination.
const response = await fetch(`${mcpBffUrl}/api/elastic/products/search`, {
method: 'POST',
headers,
body: JSON.stringify({
search: "wireless headphones", // Full-text search
filters: { status: "active" }, // Field filters
sort: { field: "createdAt", order: "desc" },
page: 1,
limit: 25,
}),
});
POST /api/elastic/:indexName/aggregate — Aggregations
Run aggregation queries for analytics and dashboards.
const response = await fetch(`${mcpBffUrl}/api/elastic/orders/aggregate`, {
method: 'POST',
headers,
body: JSON.stringify({
aggs: {
status_counts: { terms: { field: "status.keyword" } },
total_revenue: { sum: { field: "amount" } },
monthly_orders: {
date_histogram: { field: "createdAt", calendar_interval: "month" }
}
},
query: { range: { createdAt: { gte: "now-1y" } } }
}),
});
GET /api/elastic/:indexName/mapping — Index Mapping
Get the field mapping for an index (useful for building dynamic filter UIs).
const mapping = await fetch(`${mcpBffUrl}/api/elastic/products/mapping`, { headers });
POST /api/elastic/:indexName/ai-search — AI-Assisted Search
Uses the configured AI model to convert a natural-language query into an Elasticsearch query.
const response = await fetch(`${mcpBffUrl}/api/elastic/orders/ai-search`, {
method: 'POST',
headers,
body: JSON.stringify({
query: "orders over $100 from last month that are still pending",
}),
});
// Returns: { total, hits, generatedQuery, ... }
Log API
The MCP BFF provides log viewing endpoints for monitoring application behavior.
GET /api/logs — Query Logs
const response = await fetch(`${mcpBffUrl}/api/logs?page=1&limit=50&logType=2&service=commerce&search=payment`, {
headers,
});
Query Parameters:
page— Page number (default: 1)limit— Items per page (default: 50)logType— 0=INFO, 1=WARNING, 2=ERRORservice— Filter by service namesearch— Search in subject and message-
from/to— Date range (ISO strings) requestId— Filter by request ID
GET /api/logs/stream — Real-time Console Stream (SSE)
Streams real-time console output from all services via Server-Sent Events.
const eventSource = new EventSource(`${mcpBffUrl}/api/logs/stream?services=commerce,auth`, {
headers: { 'Authorization': `Bearer ${accessToken}` },
});
eventSource.addEventListener('log', (event) => {
const logEntry = JSON.parse(event.data);
// { service, timestamp, level, message, ... }
});
Available Services
The MCP BFF connects to the following backend services:
| Service | Description |
|---|---|
auth |
Authentication, user management, sessions |
adminModeration |
Admin and moderation service for logging, approval/denial, banning, role/config management, and audit actions. Orchestrates administrative and moderation business APIs, ensures every critical action is logged for traceability, and enables moderator/admin workflows. |
categoryLocation |
Manages the category and location hierarchies for listings. Provides CRUD with uniqueness enforcement, navigation endpoints for category/location trees, and supports efficient public browsing with heavy read optimization. |
conversation |
Manages user-to-user messaging threads tied to listings, with message storage, read/unread and moderation support. |
favorite |
Handles all user favorites for classified listings, including add/remove, listing user-specific collections, and providing favorited status for listings. Prevents duplicate favorites and maintains favorite counts on listings for optimal UX. Cascade-cleans favorites if user or listing is deleted. |
listing |
Manages classified listings, their lifecycle, premium features, status transitions, and provides filtering/search for marketplace ads. Integrates with users, categories, locations, and Stripe for premium ad upgrades. Enforces ad and user type business logic. |
listingImage |
Manages uploading, linking, ordering, and storing all images attached to classified listings. Enforces image file format, size, count, and metadata standards; supports multi-resolution handling and per-listing image count limits. |
payment |
Handles Stripe payment flow for one-time premium upgrades on classified listings. Creates and tracks payment transactions, manages Stripe Checkout session and webhooks, and notifies the listing service to update premium status. Exposes payment history endpoints for users and reconciliation for admin. |
Each service exposes MCP tools that the AI can call through the BFF.
Use GET /api/tools to discover all available tools at
runtime, or GET /api/tools/service/:serviceName to list
tools for a specific service.
After this prompt, the user may give you new instructions to update the output of this prompt or provide subsequent prompts about the project.