Developer Guide: Model Context Protocol (MCP) & AI Gateway Architecture (ChatGPT, Claude, Gemini, Cursor)
lynx-anchor
lynx-anchor
lynx-anchor
lynx-anchor
This document provides deep technical specifications for backend engineers, AI system architects, and full-stack developers on the Model Context Protocol (MCP), OAuth 2.0 RFC 6749 / RFC 7636 (PKCE), OpenAPI 3.0 ChatGPT Spec Generator, Gemini Function Calling Specs, and the Remote SSE / JSON-RPC 2.0 Transport Enginein Famalth Lynx.
Table of Contents
- Architecture Overview & AI Communication Flow
- OAuth 2.0 & RFC 8414 Discovery Server
- Multi-Tenant Outlet Resolution & Scoping Guard (
resolveMcpOutletContext) - Supported Transport Protocols: SSE & JSON-RPC 2.0
- Complete MCP Tool Specifications
- Dynamic Spec Generators (ChatGPT OpenAPI & Gemini Function Declarations)
- REST API Endpoint Specifications (
/api/v1/developer/*) - Testing & Verification Commands
1. Architecture Overview & AI Communication Flow
The MCP Gateway subsystem in Famalth Lynx connects Large Language Models to the relational store database while maintaining zero cross-tenant leakage:
sequenceDiagram
autonumber
participant Client as AI Client (ChatGPT / Claude / Gemini / Cursor)
participant Auth as OAuth 2.0 & PKCE Server (/developer/oauth/*)
participant Router as Developer Routes (/developer/mcp/*)
participant Ctrl as McpController (mcp.controller.ts)
participant Service as McpService (mcp.service.ts)
participant DB as Scoped propertyDb (PostgreSQL)
Client->>Auth: POST /oauth/token (grant_type=authorization_code + code_verifier)
Auth-->>Client: Returns JWT Bearer Token (scoped to outlet_id)
Client->>Router: GET /mcp/sse or POST /mcp (JSON-RPC 2.0 call_tool)
Router->>Ctrl: resolveMcpOutletContext(req)
Ctrl->>Service: executeMcpTool(toolName, arguments, outletScope)
Service->>DB: Isolated SQL Execution (WHERE outlet_id IN allowedOutletIds)
DB-->>Service: Relational Results
Service-->>Ctrl: Standardized McpResponse
Ctrl-->>Client: JSON-RPC 2.0 Result / SSE Event Stream
2. OAuth 2.0 & RFC 8414 Discovery Server
To allow OpenAI Custom GPTs and standard OAuth clients to authenticate without hardcoded secrets, Famalth Lynx implements standard RFC 6749 and RFC 7636:
Endpoints:
GET /api/v1/developer/.well-known/oauth-authorization-server:- Returns RFC 8414 discovery JSON metadata:
{
"issuer": "https://famalth-lynx-pos.onrender.com",
"authorization_endpoint": "https://famalth-lynx-pos.onrender.com/api/v1/developer/oauth/authorize",
"token_endpoint": "https://famalth-lynx-pos.onrender.com/api/v1/developer/oauth/token",
"token_endpoint_auth_methods_supported": ["client_secret_basic", "client_secret_post"],
"response_types_supported": ["code"],
"grant_types_supported": ["authorization_code", "client_credentials", "refresh_token"],
"code_challenge_methods_supported": ["S256", "plain"],
"scopes_supported": [
"orders:read",
"orders:write",
"billing:read",
"reports:read",
"inventory:read"
],
"service_documentation": "https://famalth-lynx-pos.onrender.com/api/v1/developer/ecosystem/info"
}GET/POST /api/v1/developer/oauth/authorize:- Validates
client_id, issues authorization code (lynx_auth_xxxxxxxx), and redirects back toredirect_uriwithcodeandstate.
- Validates
POST /api/v1/developer/oauth/token:- Supports
grant_type=authorization_codewith PKCEcode_verifiervalidation. - Supports
grant_type=client_credentialswith HTTP Basic Auth (Authorization: Basic <base64>) or request body credentials. - Issues a scoped JWT access token (24-hour TTL) and refresh token (30-day TTL).
- Supports
3. Multi-Tenant Outlet Resolution & Scoping Guard (resolveMcpOutletContext)
Located in backend/controllers/mcp.controller.ts:
Every AI request must pass through resolveMcpOutletContext:
export async function resolveMcpOutletContext(db: any, req: any): Promise<McpOutletContext> {
// 1. Inspect X-API-Key Header or Query Param
// 2. Inspect Bearer JWT Token (sub, outlet_id, scopes)
// 3. Query linked_outlets cluster if the outlet is a Master Headquarter
// 4. Returns strict allowedOutletIds whitelist
}
Security Policy:
- If an AI model invokes a tool with
arguments.outletId, the system validates: $$targetOutletId \in context.allowedOutletIds$$ - If an unauthorized outlet ID is requested, the system rejects execution with:
Error: Outlet #X is not within your authorized cluster (Allowed: Outlet #1, #2).
4. Supported Transport Protocols: SSE & JSON-RPC 2.0
Famalth Lynx supports both standard Model Context Protocol transports:
1. Server-Sent Events (SSE) Remote Transport (GET /api/v1/developer/mcp/sse)
- Keeps an open HTTP streaming connection (
Content-Type: text/event-stream). - Sends the initial endpoint URI event:
event: endpoint
data: /api/v1/developer/mcp/messages?sessionId=xxxxxxxx - Client sends JSON-RPC 2.0 commands via
POST /api/v1/developer/mcp/messages?sessionId=xxxxxxxx.
2. Standard JSON-RPC 2.0 over HTTP (POST /api/v1/developer/mcp)
- Implements standard MCP methods:
initialize: Returns protocol version (2024-11-05), server capabilities, and branding info.tools/list: Returns all 6 available tool schemas.tools/call: Executes the target tool and returns{ content: [{ type: 'text', text: ... }] }.
5. Complete MCP Tool Specifications
Implemented in backend/services/mcp.service.ts:
Tool 1: analyze_sales
- Purpose: Computes gross sales, net revenue, discounts, taxes, invoice count, average order value (AOV), payment method splits, hourly peak sales, and top-selling items.
- Parameters:
timeframe:today|yesterday|this_week|last_7_days|this_month|last_month|custom.startDate,endDate:YYYY-MM-DD(for custom range).groupBy:day|payment_mode|hourly|top_items.outletId: Target outlet.
Tool 2: create_order_bill
- Purpose: Creates an atomic sales order in
sales_headersandsales_items, updatesstock_ledger, logs cash/bank balances, and outputs a formatted POS receipt text. - Parameters:
customerName: string.customerPhone: string.paymentMode:CASH|UPI|CARD|CREDIT|SPLIT.items: Array of{ name, quantity, unitPrice?, itemCode?, barcode?, discount? }.discountAmount: Overall bill discount.notes: e.g.Order generated via ChatGPT Assistant.outletId: Target outlet.
Tool 3: get_inventory_status
- Purpose: Checks live stock balances, calculates total inventory valuation (cost vs retail), and lists items below threshold.
- Parameters:
filter:all|low_stock|out_of_stock.threshold: Low-stock warning threshold (default: 10).search: Keyword filter.limit: Integer (default: 50, max: 200).
Tool 4: search_products
- Purpose: Searches
item_masterby product name, SKU, barcode, or category keyword. - Parameters:
query: Keyword string.limit: Maximum items to return (default: 20).
Tool 5: get_order_details
- Purpose: Retrieves complete itemized invoice information.
- Parameters:
billNumber: e.g.BILL_1042.orderId: Numeric primary key.
Tool 6: get_customer_insights
- Purpose: Calculates customer RFM (Recency, Frequency, Monetary) metrics, lifetime orders, total spend, and unpaid credit balances.
- Parameters:
phone: Mobile number.name: Customer name search fallback.
Extended Full ERP Tool Suite (backend/services/mcpErpTools.service.ts)
In addition to the 6 core POS tools in mcp.service.ts, mcpErpTools.service.ts registers 32 enterprise ERP tools (38 total tools) with full multi-tenant outlet scoping (allowedOutletIds) and transactional integrity:
| Domain | Tool Name | Purpose & Key Capabilities |
|---|---|---|
| Store Profile | get_outlet_profile | Returns outlet metadata, GSTIN, address, currency, and active module flags. |
| Accounting & Reports | get_accounting_reports | Computes Profit & Loss (P&L), Trial Balance, Balance Sheet, Cash Ledger, Vouchers & Expenses. |
| Accounting & Ledgers | manage_bank_and_capital, record_finance_entry, get_party_ledgers | Manages bank accounts/capital, posts balanced double-entry vouchers/expenses, and reads party aging ledgers. |
| Procurement & Vendor Settlement | create_purchase_order, approve_purchase_order, create_supplier, list_supplier_bills, pay_supplier_bill, create_vendor_return | Drafts and approves vendor Purchase Orders (PO-), lists payable bills, settles supplier invoices, and issues debit notes. |
| Warehouse, GRN & Inventory | create_receiving, create_stock_dispatch, create_material_requisition, create_department_return, create_damage_entry | Records Goods Receiving Notes (RC-), stock dispatches (INT-), requisitions (REC-), department returns, and damage write-offs (DMG-). |
| Restaurant & Dining | create_table_order | Dispatches live KOT tickets (KOT-) to kitchen screens for dine-in floor tables. |
| CRM & Loyalty | create_customer, manage_loyalty_program, manage_lucky_draw_campaign, manage_pricing_and_promos, manage_sales_schemes | Registers customers, configures reward points tiers, launches raffle campaigns, and manages promotional rules/schemes. |
| HRMS | get_attendance, create_employee | Queries staff attendance punches/shifts and creates employee profiles. |
| Master Data & Taxes | create_master_record, list_master_records, update_master_record, create_tax_group, manage_payment_methods | Generic CRUD across item_master, supplier_master, and hr_employees, plus tax group and payment method configuration. |
| System & Backups | create_outlet_backup, manage_plugins, create_subscription | Triggers scoped .posbak outlet backups, manages installed plugins, and creates recurring delivery subscriptions. |
MCP Prompts (prompts/list, prompts/get)
mcp.service.ts exposes two built-in system prompts over JSON-RPC:
retail_analyst: Acceptstimeframeargument and primes the LLM as a Famalth Lynx retail data analyst.pos_cashier: Primes the LLM for accurate billing, tax validation, and customer lookup.
6. Dynamic Spec Generators (ChatGPT OpenAPI & Gemini Function Declarations)
The server automatically compiles and serves runtime specs dynamically matched to the server's current host domain and SSL protocol:
- ChatGPT OpenAPI 3.0 Spec (
GET /api/v1/developer/mcp/chatgpt-openapi.json):- Dynamically resolves
servers: [{ url: "https://<current-host>/api/v1/developer" }]. - Defines OpenAPI paths for
/mcp/tools/analyze-sales,/create-order-bill,/inventory-status, and/search-products. - Defines
securitySchemessupporting bothOAuth2(Authorization Code + PKCE) andApiKeyAuth(Custom headerX-API-Key). - Enables instant 1-click import into ChatGPT GPT Builder under Actions$\rightarrow$ Import from URL.
- Dynamically resolves
- Gemini Function Calling Spec (
GET /api/v1/developer/mcp/gemini-tools.json):- Formats tools into the Google Generative AI SDK function declaration schema (
functionDeclarations: [...]). - Compatible with Google AI Studio, Vertex AI, and Gemini Live API.
- Formats tools into the Google Generative AI SDK function declaration schema (
7. REST API Endpoint Specifications (/api/v1/developer/*)
Mounted on /api/v1/developer via backend/routes/developer.routes.ts:
API Keys & OAuth
GET /api/v1/developer/api-keys- List active developer credentials.POST /api/v1/developer/api-keys- Issue new API Key & Client Secret.DELETE /api/v1/developer/api-keys/:id- Revoke developer credentials.GET/POST /api/v1/developer/oauth/authorize- OAuth 2.0 authorization code issuance.POST /api/v1/developer/oauth/token- OAuth 2.0 token exchange (code exchange & client credentials).GET /api/v1/developer/.well-known/oauth-authorization-server- RFC 8414 server metadata.
Model Context Protocol (MCP) Transports
POST /api/v1/developer/mcp- Standard JSON-RPC 2.0 endpoint.GET /api/v1/developer/mcp/sse- Server-Sent Events stream for remote MCP clients.POST /api/v1/developer/mcp/messages- SSE message delivery endpoint.GET /api/v1/developer/mcp/tools- JSON list of all available tool schemas.GET /api/v1/developer/mcp/config- Server diagnostics, uptime, and MCP capabilities.GET /api/v1/developer/mcp/chatgpt-openapi.json- OpenAPI 3.0 specification for Custom GPTs.GET /api/v1/developer/mcp/gemini-tools.json- Google Gemini tool calling declarations.
Direct Action Endpoints
POST /api/v1/developer/mcp/tools/execute- Execute any tool directly by name.POST /api/v1/developer/mcp/tools/analyze-sales- Direct sales analytics execution.POST /api/v1/developer/mcp/tools/create-order-bill- Direct POS order billing.POST /api/v1/developer/mcp/tools/inventory-status- Direct stock status execution.POST /api/v1/developer/mcp/tools/search-products- Direct product search.
8. Testing & Verification Commands
1. Test JSON-RPC 2.0 Tool Listing:
curl -X POST http://localhost:5000/api/v1/developer/mcp \
-H "X-API-Key: YOUR_LYNX_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/list"
}'
2. Test JSON-RPC 2.0 Tool Call (analyze_sales):
curl -X POST http://localhost:5000/api/v1/developer/mcp \
-H "X-API-Key: YOUR_LYNX_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "analyze_sales",
"arguments": {
"timeframe": "today"
}
}
}'
3. Test OAuth 2.0 Token Generation via Client Credentials:
curl -X POST http://localhost:5000/api/v1/developer/oauth/token \
-H "Content-Type: application/json" \
-d '{
"grant_type": "client_credentials",
"client_id": "YOUR_CLIENT_ID",
"client_secret": "YOUR_CLIENT_SECRET"
}'
4. Test Remote SSE Connection:
curl -N -H "X-API-Key: YOUR_LYNX_API_KEY" http://localhost:5000/api/v1/developer/mcp/sse
Expected: Streams event: endpoint with unique session callback URL.