airweave / rules
Cursor rule6.6k starsChanged 8 months agoMITArchived repositoryView on GitHub
MCP (Model Context Protocol) server architecture and implementation
- Reads credentials
Where files are headingNew Cursor rule files have more rules about planning and tracking tasks, and fewer about code style.
Of the Cursor rule files we read, the 1,533 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repository22 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- .cursor/rules/airweave-overview.mdcCursor rule
- .cursor/rules/api-layer.mdcCursor rule
- .cursor/rules/arf.mdcCursor rule
- .cursor/rules/auth-providers.mdcCursor rule
- .cursor/rules/backend-rules.mdcCursor rule
- .cursor/rules/connector-cursors.mdcCursor rule
- .cursor/rules/connector-development-end-to-end.mdcCursor rule
- .cursor/rules/connect-widget.mdcCursor rule
- .cursor/rules/crud-layer.mdcCursor rule
- .cursor/rules/feature-flags.mdcCursor rule
- .cursor/rules/fern-docs-generation.mdcCursor rule
- .cursor/rules/form-validation.mdcCursor rule
- .cursor/rules/frontend-rules.mdcCursor rule
- .cursor/rules/integrations-yaml.mdcCursor rule
- .cursor/rules/monke.mdcCursor rule
- .cursor/rules/monke-testing-guide.mdcCursor rule
- .cursor/rules/search-module.mdcCursor rule
- .cursor/rules/source-connector-implementation.mdcCursor rule
- .cursor/rules/stripe-billing.mdcCursor rule
- .cursor/rules/sync-architecture.mdcCursor rule
- .cursor/rules/token-provider-exceptions.mdcCursor rule
The file
We split this mcp-server.mdc into the rules it gives an agent
4 rules. About 0 are common, 2 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 152
All external dependencies are mocked (vi.mock): Redis, fetch, jose
- Distinctive: line 162
No sessions — every request is independent
- Very specific: line 168
OAuth gating: all OAuth code paths check oauthEnabled flag
- Distinctive: line 169
Org resolution: only runs for OAuth requests (API key users have org context built-in)
≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
---
description: MCP (Model Context Protocol) server architecture and implementation
globs: **/mcp/**
alwaysApply: false
---
# Airweave MCP Server Architecture
## Overview
The Airweave MCP Server provides search capabilities for AI assistants through the Model Context Protocol (MCP). It supports both local desktop clients and cloud-based AI platforms, with API key and OAuth 2.0 authentication.
## Deployment Modes
### 1. Local Mode (Desktop AI Clients)
- **Transport**: stdio (standard input/output)
- **Target**: Claude Desktop, Cursor, VS Code
- **Entry Point**: `src/index.ts`
- **Installation**: `npx airweave-mcp-search`
- **Architecture**: Single-tenant, one server instance per user
- **Authentication**: API key from environment variables
### 2. Hosted Mode (Cloud AI Platforms)
- **Transport**: Streamable HTTP (MCP 2025-03-26)
- **Target**: OpenAI Agent Builder, Cursor (remote), any HTTP MCP client
- **Entry Point**: `src/index-http.ts`
- **Deployment**: Azure Kubernetes Service
- **URL**: `https://mcp.airweave.ai/mcp`
- **Architecture**: Fully stateless — a fresh McpServer + transport is created per request
- **Authentication**: API key (via headers) or OAuth 2.0 (via Auth0)
## Architecture Components
### Core Server (`src/server.ts`)
- **McpServer**: MCP server factory — `createMcpServer(config)` returns an isolated instance
- **Tool Registration**: Dynamic tool creation based on collection (`search-{collection}`, `get-config`)
- **Airweave Client**: API client for search operations (`src/api/airweave-client.ts`)
### Streamable HTTP Server (`src/index-http.ts`)
- **Express App**: HTTP server with health checks and info endpoints
- **Streamable HTTP Transport**: MCP 2025-03-26 transport, stateless (no sessions)
- **Request Handling**: POST `/mcp` — creates fresh McpServer per request
- **Dual Auth**: `resolveAuth` middleware resolves API key vs OAuth per request
- **OAuth Router**: `mcpAuthRouter` from MCP SDK handles OAuth protocol endpoints when enabled
### Authentication (`src/auth/`)
#### `resolveAuth` middleware (in `index-http.ts`)
Priority-based auth resolution per request:
1. `X-API-Key` header present → API key auth, no JWT check
2. `Authorization: Bearer <token>` with OAuth enabled → attempt JWT verification via `Auth0OAuthProvider.verifyAccessToken`; if valid → OAuth path (`req.auth` set); if invalid → fallback to API key
3. No credential → 401
#### Auth0 OAuth Provider (`src/auth/auth0-provider.ts`)
- Implements `OAuthServerProvider` from the MCP SDK
- Handles: `authorize` (redirect to Auth0), `exchangeAuthorizationCode`, `exchangeRefreshToken`, `verifyAccessToken` (JWKS-based JWT verification), `revokeToken`
- Uses `jose` library for JWT verification with JWKS auto-discovery
- Config via env vars: `AUTH0_DOMAIN`, `AUTH0_CLIENT_ID`, `AUTH0_CLIENT_SECRET`, `AUTH0_AUDIENCE`
#### Auth0 Callback (`src/auth/auth0-callback.ts`)
- Express handler for `/oauth/callback`
- Receives Auth0 authorization code, exchanges it for a local authorization code via the provider
- Redirects back to the MCP client's `redirect_uri` with the local code
#### OAuth Transaction Store (`src/auth/oauth-transaction-store.ts`)
- Redis-backed store for pending OAuth authorizations and authorization codes
- TTL-based expiry (10 min for pending auths, 10 min for codes)
- Atomic code consumption (Redis `GET` + `DEL`)
#### Registered Clients Store (`src/auth/registered-clients-store.ts`)
- Redis-backed store for dynamically registered OAuth clients (RFC 7591)
- 7-day TTL — stateless MCP means clients re-register naturally after expiry
#### Redis Client (`src/auth/redis.ts`)
- Singleton Redis client with lazy connect
- `getRedisClient()`, `ensureRedisReady()`, `disconnectRedis()`
- URL built from `MCP_REDIS_URL` or `REDIS_HOST`/`REDIS_PORT`/`REDIS_PASSWORD`
#### Security Utilities (`src/auth/security.ts`)
- `redactSensitiveValue`: masks tokens/secrets for logging
- `hashIdentifier`: SHA-256 hashing for identifiers
- `safeLogObject`: recursively redacts sensitive keys in objects
### Organization Resolver (`src/api/org-resolver.ts`)
- Resolves which organization owns a collection for OAuth users
- Fetches user's orgs, probes each in parallel (`Promise.all`) for the target collection
- LRU in-memory cache (max 500 entries, 5-min TTL) keyed by `hash(token):collection`
### Tools
- **Search Tool**: `search-{collection}` — full search with query, filters, pagination, reranking
- **Config Tool**: `get-config` — server configuration display
## Configuration
### Environment Variables
**Core (both modes):**
- `AIRWEAVE_COLLECTION`: Default collection readable ID
- `AIRWEAVE_BASE_URL`: API base URL (default: `https://api.airweave.ai`)
- `PORT`: Server port (default: 8080)
**Local mode only:**
- `AIRWEAVE_API_KEY`: API key for authentication
**OAuth (hosted mode, when `MCP_OAUTH_ENABLED=true`):**
- `MCP_OAUTH_ENABLED`: Set to `true` to enable OAuth 2.0
- `MCP_BASE_URL`: Public URL of the MCP server (used for OAuth metadata/redirects)
- `AUTH0_DOMAIN`: Auth0 tenant domain
- `AUTH0_AUDIENCE`: Auth0 API audience identifier
- `AUTH0_CLIENT_ID`: Auth0 application client ID
- `AUTH0_CLIENT_SECRET`: Auth0 application client secret
- `REDIS_HOST`: Redis host (default: `localhost`)
- `REDIS_PORT`: Redis port (default: `6379`)
- `REDIS_PASSWORD`: Redis password (optional)
- `MCP_REDIS_URL`: Full Redis URL (overrides individual REDIS_* vars)
## OAuth Flow
```
MCP Client (Cursor/mcp-remote)
│
├─ GET /.well-known/oauth-authorization-server → discover endpoints
├─ POST /register → dynamic client registration
├─ GET /authorize → redirect to Auth0 login
│ └─ Auth0 login page
│ └─ POST /oauth/callback → exchange Auth0 code for local code
├─ POST /token → exchange local code for Auth0 tokens
└─ POST /mcp (Authorization: Bearer <jwt>) → authenticated MCP request
```
## Testing
### Test Files
- `tests/mcp-server.test.ts` — core MCP functionality, tool registration, parameter validation
- `tests/http-transport.test.ts` — stateless HTTP transport, collection headers
- `tests/auth/auth0-provider.test.ts` — OAuth provider: authorize, token exchange, JWT verification, revocation
- `tests/auth/auth0-callback.test.ts` — callback handler: param validation, redirects, error handling
- `tests/auth/oauth-transaction-store.test.ts` — pending auth CRUD, code issuance/consumption, TTLs
- `tests/auth/registered-clients-store.test.ts` — client registration, retrieval, TTL expiry
- `tests/auth/redis.test.ts` — singleton lifecycle, URL construction, connect/disconnect
- `tests/auth/security.test.ts` — redaction, hashing, safe logging
- `tests/api/org-resolver.test.ts` — org resolution, parallel probing, LRU cache eviction
- `tests/index-http-oauth.test.ts` — dual-auth integration: API key priority, JWT verification, fallback
### Test Commands
- `npm run test:all` — run everything
- `npm run test:mcp` — core MCP tests only
- `npm run test:http` — HTTP transport tests only
- `npm run test:oauth` — OAuth + org-resolver tests only
- `npm run test:coverage` — full suite with coverage report
### Test Patterns
- All external dependencies are mocked (`vi.mock`): Redis, fetch, jose
- Auth0 provider tests mock JWKS/JWT verification via `jose`
- Org resolver tests mock fetch responses for orgs and collection probes
- OAuth integration tests replicate the Express app with `supertest`
## Development Guidelines
### Code Structure
- TypeScript with strict typing
- Async/await for all I/O
- No sessions — every request is independent
- `express.Request` extended with `_authMethod` and `auth` for dual-auth
### Key Patterns
- **Stateless requests**: fresh `McpServer` + `StreamableHTTPServerTransport` per POST
- **Auth resolution**: `resolveAuth` middleware runs before `/mcp` handler
- **OAuth gating**: all OAuth code paths check `oauthEnabled` flag
- **Org resolution**: only runs for OAuth requests (API key users have org context built-in)
### Deployment
- Docker containerization
- Kubernetes deployment (AKS)
- Health check at `/health`
- Graceful shutdown: `disconnectRedis()` + `shutdownPostHog()` on SIGTERM/SIGINT
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

