Skip to content

Commit ab24838

Browse files
authored
📚 docs: MCP server Dynamic User Field Placeholders (#326)
- Updated the `mcp_servers.mdx` and `agents.mdx` files to include dynamic user field placeholders for headers, URLs, and environment variables. - Added detailed descriptions and examples for user field placeholders such as `{{LIBRECHAT_USER_EMAIL}}`, `{{LIBRECHAT_USER_ROLE}}`, and others. - Improved clarity on the usage of environment variables and their integration with user-specific data.
1 parent 9908957 commit ab24838

File tree

2 files changed

+76
-7
lines changed

2 files changed

+76
-7
lines changed

‎pages/docs/configuration/librechat_yaml/object_structure/mcp_servers.mdx

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ mcpServers:
5656
['command', 'String', '(For `stdio` type) The command or executable to run to start the MCP server.', 'command: "npx"'],
5757
['args', 'Array of Strings', '(For `stdio` type) Command line arguments to pass to the `command`.', 'args: ["-y", "@modelcontextprotocol/server-puppeteer"]'],
5858
['url', 'String', '(For `websocket`, `streamable-http`, or `sse` type) The URL to connect to the MCP server.', 'url: "http://localhost:3001/sse"'],
59-
['headers', 'Object', '(Optional, for `sse` and `streamable-http` types) Custom headers to send with the request. Supports user ID substitution with `{{LIBRECHAT_USER_ID}}` and environment variables with `${ENV_VAR}`.', 'headers:\n X-User-ID: "{{LIBRECHAT_USER_ID}}"\n X-API-Key: "${SOME_API_KEY}"'],
59+
['headers', 'Object', '(Optional, for `sse` and `streamable-http` types) Custom headers to send with the request. Supports dynamic user field substitution with `{{LIBRECHAT_USER_*}}` placeholders and environment variables with `${ENV_VAR}`.', 'headers:\n X-User-ID: "{{LIBRECHAT_USER_ID}}"\n X-API-Key: "${SOME_API_KEY}"'],
6060
['iconPath', 'String', '(Optional) Defines the tool\'s display icon shown in the tool selection dialog.', 'iconPath: "/path/to/icon.svg"'],
6161
['chatMenu', 'Boolean', '(Optional) When set to `false`, excludes the MCP server from the chatarea dropdown (MCPSelect) for quick and easy access. Defaults to `true`.', 'chatMenu: false'],
6262
['timeout', 'Number', '(Optional) Timeout in milliseconds for MCP server requests. Determines how long to wait for a response for tool requests.', 'timeout: 30000'],
@@ -85,7 +85,7 @@ mcpServers:
8585
#### `url`
8686

8787
- **Type:** String
88-
- **Description:** (For `websocket`, `streamable-http`, or `sse` type) The URL to connect to the MCP server.
88+
- **Description:** (For `websocket`, `streamable-http`, or `sse` type) The URL to connect to the MCP server. Supports dynamic user field placeholders (`{{LIBRECHAT_USER_*}}`) and environment variable substitution (`${ENV_VAR}`).
8989
- **Notes:**
9090
- For `sse` type, the URL must start with `http://` or `https://`.
9191
- For `streamable-http` type, the URL must start with `http://` or `https://`.
@@ -97,11 +97,38 @@ mcpServers:
9797
- **Description:** Custom headers to send with the request.
9898
- **Special Values:**
9999
- `{{LIBRECHAT_USER_ID}}`: Will be replaced with the current user's ID, enabling multi-user support.
100+
- `{{LIBRECHAT_USER_*}}`: Dynamic user field placeholders. Replace `*` with the UPPERCASE version of any allowed field.
100101
- `${ENV_VAR}`: Will be replaced with the value of the environment variable `ENV_VAR`.
102+
103+
**Available User Field Placeholders:**
104+
105+
| Placeholder | User Field | Type | Description |
106+
|------------|------------|------|-------------|
107+
| `{{LIBRECHAT_USER_NAME}}` | `name` | String | User's display name |
108+
| `{{LIBRECHAT_USER_USERNAME}}` | `username` | String | User's username |
109+
| `{{LIBRECHAT_USER_EMAIL}}` | `email` | String | User's email address |
110+
| `{{LIBRECHAT_USER_PROVIDER}}` | `provider` | String | Authentication provider (e.g., "email", "google", "github") |
111+
| `{{LIBRECHAT_USER_ROLE}}` | `role` | String | User's role (e.g., "user", "admin") |
112+
| `{{LIBRECHAT_USER_GOOGLEID}}` | `googleId` | String | Google account ID |
113+
| `{{LIBRECHAT_USER_FACEBOOKID}}` | `facebookId` | String | Facebook account ID |
114+
| `{{LIBRECHAT_USER_OPENIDID}}` | `openidId` | String | OpenID account ID |
115+
| `{{LIBRECHAT_USER_SAMLID}}` | `samlId` | String | SAML account ID |
116+
| `{{LIBRECHAT_USER_LDAPID}}` | `ldapId` | String | LDAP account ID |
117+
| `{{LIBRECHAT_USER_GITHUBID}}` | `githubId` | String | GitHub account ID |
118+
| `{{LIBRECHAT_USER_DISCORDID}}` | `discordId` | String | Discord account ID |
119+
| `{{LIBRECHAT_USER_APPLEID}}` | `appleId` | String | Apple account ID |
120+
| `{{LIBRECHAT_USER_EMAILVERIFIED}}` | `emailVerified` | Boolean → String | Email verification status ("true" or "false") |
121+
| `{{LIBRECHAT_USER_TWOFACTORENABLED}}` | `twoFactorEnabled` | Boolean → String | 2FA status ("true" or "false") |
122+
| `{{LIBRECHAT_USER_TERMSACCEPTED}}` | `termsAccepted` | Boolean → String | Terms acceptance status ("true" or "false") |
123+
124+
**Note:** Missing fields will be replaced with empty strings.
125+
101126
- **Example:**
102127
```yaml
103128
headers:
104129
X-User-ID: "{{LIBRECHAT_USER_ID}}"
130+
X-User-Email: "{{LIBRECHAT_USER_EMAIL}}"
131+
X-User-Role: "{{LIBRECHAT_USER_ROLE}}"
105132
X-API-Key: "${SOME_API_KEY}"
106133
Authorization: "Bearer ${SOME_AUTH_TOKEN}"
107134
```
@@ -120,7 +147,7 @@ mcpServers:
120147
#### `env`
121148

122149
- **Type:** Object (Optional, `stdio` type only)
123-
- **Description:** Environment variables to use when spawning the process.
150+
- **Description:** Environment variables to use when spawning the process. Supports the same dynamic user field placeholders as headers (`{{LIBRECHAT_USER_*}}`) and environment variable substitution (`${ENV_VAR}`).
124151

125152
#### `timeout`
126153

@@ -168,6 +195,8 @@ puppeteer:
168195
initTimeout: 10000
169196
env:
170197
NODE_ENV: "production"
198+
USER_EMAIL: "{{LIBRECHAT_USER_EMAIL}}"
199+
USER_ROLE: "{{LIBRECHAT_USER_ROLE}}"
171200
stderr: inherit
172201
```
173202

@@ -199,6 +228,20 @@ streamable-http-server:
199228
X-API-Key: "${SOME_API_KEY}"
200229
```
201230

231+
### MCP Server with Dynamic User Fields
232+
233+
```yaml filename="MCP Server with User Fields"
234+
user-aware-server:
235+
type: sse
236+
url: https://api.example.com/users/{{LIBRECHAT_USER_USERNAME}}/stream
237+
headers:
238+
X-User-ID: "{{LIBRECHAT_USER_ID}}"
239+
X-User-Email: "{{LIBRECHAT_USER_EMAIL}}"
240+
X-User-Role: "{{LIBRECHAT_USER_ROLE}}"
241+
X-Email-Verified: "{{LIBRECHAT_USER_EMAILVERIFIED}}"
242+
Authorization: "Bearer ${API_TOKEN}"
243+
```
244+
202245
### MCP Server with Custom Icon
203246

204247
```yaml filename="MCP Server with Icon"
@@ -228,12 +271,22 @@ The `mcpServers` configurations allow LibreChat to dynamically interact with var
228271
- **Multi-User Support:**
229272
- The MCPManager now supports distinct user-level and app-level connections, enabling proper connection management per user.
230273
- User connections are tracked and managed separately, with proper establishment and cleanup.
231-
- Use the `{{LIBRECHAT_USER_ID}}` placeholder in headers to identify user-specific connections.
274+
- Use dynamic user field placeholders in headers, URLs, and environment variables:
275+
- `{{LIBRECHAT_USER_ID}}` - User's unique identifier
276+
- `{{LIBRECHAT_USER_EMAIL}}` - User's email address
277+
- `{{LIBRECHAT_USER_USERNAME}}` - User's username
278+
- `{{LIBRECHAT_USER_ROLE}}` - User's role (e.g., "user", "admin")
279+
- And many more fields (see headers section for complete list)
232280
- **User Idle Management:**
233281
- User connections are monitored for activity and will be disconnected after 15 minutes of inactivity.
234282
- **Environment Variables:**
235283
- **In `env` (for `stdio` type):** Useful for setting up specific runtime environments or configurations required by the MCP server process.
236284
- **In `headers` (for `sse` and `streamable-http` types):** Use `${ENV_VAR}` syntax to reference environment variables in header values.
285+
- **Dynamic User Fields:**
286+
- User field placeholders are replaced at runtime with the authenticated user's information
287+
- Only non-sensitive fields are available (passwords and other sensitive data are excluded)
288+
- Missing fields default to empty strings
289+
- Boolean fields are converted to string representations ("true" or "false")
237290
- **Error Handling (`stderr`):**
238291
- Configuring `stderr` allows you to manage how error messages from the MCP server process are handled. The default `"inherit"` means that the errors will be printed to the parent process's `stderr`.
239292

‎pages/docs/features/agents.mdx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,13 @@ MCP servers can be configured to use different transport mechanisms:
220220
**Multi-User Features**
221221
- User-level and app-level connections with proper establishment and cleanup
222222
- MCP Servers can distinguish connections by session ID
223-
- Custom headers with user ID substitution: `{{LIBRECHAT_USER_ID}}`
224-
- Environment variable support in headers: `${SOME_API_KEY}`
223+
- Dynamic user field placeholders in headers, URLs, and environment variables:
224+
- `{{LIBRECHAT_USER_ID}}` - User's unique identifier
225+
- Basic fields: `{{LIBRECHAT_USER_EMAIL}}`, `{{LIBRECHAT_USER_USERNAME}}`, `{{LIBRECHAT_USER_NAME}}`, `{{LIBRECHAT_USER_ROLE}}`, `{{LIBRECHAT_USER_PROVIDER}}`
226+
- Provider IDs: `{{LIBRECHAT_USER_GOOGLEID}}`, `{{LIBRECHAT_USER_OPENIDID}}`, `{{LIBRECHAT_USER_SAMLID}}`, `{{LIBRECHAT_USER_LDAPID}}`, etc.
227+
- Boolean fields: `{{LIBRECHAT_USER_EMAILVERIFIED}}`, `{{LIBRECHAT_USER_TWOFACTORENABLED}}`, `{{LIBRECHAT_USER_TERMSACCEPTED}}`
228+
- See [MCP Servers documentation](/docs/configuration/librechat_yaml/object_structure/mcp_servers#headers) for complete field list
229+
- Environment variable support: `${SOME_API_KEY}`
225230
- User idle timeout management for efficient resource usage
226231

227232
**Example Configuration:**
@@ -232,14 +237,25 @@ mcpServers:
232237
url: https://mcp.composio.dev/googlesheets/some-endpoint
233238
headers:
234239
X-User-ID: "{{LIBRECHAT_USER_ID}}"
240+
X-User-Email: "{{LIBRECHAT_USER_EMAIL}}"
235241
X-API-Key: "${SOME_API_KEY}"
236242

237243
streamable-http-example:
238244
type: streamable-http
239-
url: https://example.com/mcp/
245+
url: https://example.com/mcp/users/{{LIBRECHAT_USER_USERNAME}}
240246
headers:
241247
X-User-ID: "{{LIBRECHAT_USER_ID}}"
248+
X-User-Role: "{{LIBRECHAT_USER_ROLE}}"
242249
Authorization: "Bearer ${API_TOKEN}"
250+
251+
user-context-server:
252+
type: stdio
253+
command: node
254+
args: ["server.js"]
255+
env:
256+
USER_EMAIL: "{{LIBRECHAT_USER_EMAIL}}"
257+
USER_PROVIDER: "{{LIBRECHAT_USER_PROVIDER}}"
258+
API_KEY: "${MCP_API_KEY}"
243259
```
244260
245261
LibreChat is at the forefront of implementing flexible, scalable MCP server integrations to support diverse usage scenarios.

0 commit comments

Comments
 (0)