Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bcbf889

Browse files
authored
feat(elicitation): add option to require user confirmation for tool calls with elicitation MCP-185 (#551)
Adds an option to require confirmation for certain tools using the new elicitation API. This is not supported by most popular clients yet, only notably VSCode.
1 parent e71e2ba commit bcbf889

File tree

19 files changed

+931
-45
lines changed

19 files changed

+931
-45
lines changed

README.md

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -338,26 +338,27 @@ The MongoDB MCP Server can be configured using multiple methods, with the follow
338338

339339
### Configuration Options
340340

341-
| CLI Option | Environment Variable | Default | Description |
342-
| -------------------------------------- | --------------------------------------------------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
343-
| `apiClientId` | `MDB_MCP_API_CLIENT_ID` | <not set> | Atlas API client ID for authentication. Required for running Atlas tools. |
344-
| `apiClientSecret` | `MDB_MCP_API_CLIENT_SECRET` | <not set> | Atlas API client secret for authentication. Required for running Atlas tools. |
345-
| `connectionString` | `MDB_MCP_CONNECTION_STRING` | <not set> | MongoDB connection string for direct database connections. Optional, if not set, you'll need to call the `connect` tool before interacting with MongoDB data. |
346-
| `loggers` | `MDB_MCP_LOGGERS` | disk,mcp | Comma separated values, possible values are `mcp`, `disk` and `stderr`. See [Logger Options](#logger-options) for details. |
347-
| `logPath` | `MDB_MCP_LOG_PATH` | see note\* | Folder to store logs. |
348-
| `disabledTools` | `MDB_MCP_DISABLED_TOOLS` | <not set> | An array of tool names, operation types, and/or categories of tools that will be disabled. |
349-
| `readOnly` | `MDB_MCP_READ_ONLY` | false | When set to true, only allows read, connect, and metadata operation types, disabling create/update/delete operations. |
350-
| `indexCheck` | `MDB_MCP_INDEX_CHECK` | false | When set to true, enforces that query operations must use an index, rejecting queries that perform a collection scan. |
351-
| `telemetry` | `MDB_MCP_TELEMETRY` | enabled | When set to disabled, disables telemetry collection. |
352-
| `transport` | `MDB_MCP_TRANSPORT` | stdio | Either 'stdio' or 'http'. |
353-
| `httpPort` | `MDB_MCP_HTTP_PORT` | 3000 | Port number. |
354-
| `httpHost` | `MDB_MCP_HTTP_HOST` | 127.0.0.1 | Host to bind the http server. |
355-
| `idleTimeoutMs` | `MDB_MCP_IDLE_TIMEOUT_MS` | 600000 | Idle timeout for a client to disconnect (only applies to http transport). |
356-
| `notificationTimeoutMs` | `MDB_MCP_NOTIFICATION_TIMEOUT_MS` | 540000 | Notification timeout for a client to be aware of diconnect (only applies to http transport). |
357-
| `exportsPath` | `MDB_MCP_EXPORTS_PATH` | see note\* | Folder to store exported data files. |
358-
| `exportTimeoutMs` | `MDB_MCP_EXPORT_TIMEOUT_MS` | 300000 | Time in milliseconds after which an export is considered expired and eligible for cleanup. |
359-
| `exportCleanupIntervalMs` | `MDB_MCP_EXPORT_CLEANUP_INTERVAL_MS` | 120000 | Time in milliseconds between export cleanup cycles that remove expired export files. |
360-
| `atlasTemporaryDatabaseUserLifetimeMs` | `MDB_MCP_ATLAS_TEMPORARY_DATABASE_USER_LIFETIME_MS` | 14400000 | Time in milliseconds that temporary database users created when connecting to MongoDB Atlas clusters will remain active before being automatically deleted. |
341+
| CLI Option | Environment Variable | Default | Description |
342+
| -------------------------------------- | --------------------------------------------------- | --------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
343+
| `apiClientId` | `MDB_MCP_API_CLIENT_ID` | <not set> | Atlas API client ID for authentication. Required for running Atlas tools. |
344+
| `apiClientSecret` | `MDB_MCP_API_CLIENT_SECRET` | <not set> | Atlas API client secret for authentication. Required for running Atlas tools. |
345+
| `connectionString` | `MDB_MCP_CONNECTION_STRING` | <not set> | MongoDB connection string for direct database connections. Optional, if not set, you'll need to call the `connect` tool before interacting with MongoDB data. |
346+
| `loggers` | `MDB_MCP_LOGGERS` | disk,mcp | Comma separated values, possible values are `mcp`, `disk` and `stderr`. See [Logger Options](#logger-options) for details. |
347+
| `logPath` | `MDB_MCP_LOG_PATH` | see note\* | Folder to store logs. |
348+
| `disabledTools` | `MDB_MCP_DISABLED_TOOLS` | <not set> | An array of tool names, operation types, and/or categories of tools that will be disabled. |
349+
| `confirmationRequiredTools` | `MDB_MCP_CONFIRMATION_REQUIRED_TOOLS` | create-access-list,create-db-user,drop-database,drop-collection,delete-many | An array of tool names that require user confirmation before execution. **Requires the client to support [elicitation](https://modelcontextprotocol.io/specification/draft/client/elicitation)**. |
350+
| `readOnly` | `MDB_MCP_READ_ONLY` | false | When set to true, only allows read, connect, and metadata operation types, disabling create/update/delete operations. |
351+
| `indexCheck` | `MDB_MCP_INDEX_CHECK` | false | When set to true, enforces that query operations must use an index, rejecting queries that perform a collection scan. |
352+
| `telemetry` | `MDB_MCP_TELEMETRY` | enabled | When set to disabled, disables telemetry collection. |
353+
| `transport` | `MDB_MCP_TRANSPORT` | stdio | Either 'stdio' or 'http'. |
354+
| `httpPort` | `MDB_MCP_HTTP_PORT` | 3000 | Port number. |
355+
| `httpHost` | `MDB_MCP_HTTP_HOST` | 127.0.0.1 | Host to bind the http server. |
356+
| `idleTimeoutMs` | `MDB_MCP_IDLE_TIMEOUT_MS` | 600000 | Idle timeout for a client to disconnect (only applies to http transport). |
357+
| `notificationTimeoutMs` | `MDB_MCP_NOTIFICATION_TIMEOUT_MS` | 540000 | Notification timeout for a client to be aware of diconnect (only applies to http transport). |
358+
| `exportsPath` | `MDB_MCP_EXPORTS_PATH` | see note\* | Folder to store exported data files. |
359+
| `exportTimeoutMs` | `MDB_MCP_EXPORT_TIMEOUT_MS` | 300000 | Time in milliseconds after which an export is considered expired and eligible for cleanup. |
360+
| `exportCleanupIntervalMs` | `MDB_MCP_EXPORT_CLEANUP_INTERVAL_MS` | 120000 | Time in milliseconds between export cleanup cycles that remove expired export files. |
361+
| `atlasTemporaryDatabaseUserLifetimeMs` | `MDB_MCP_ATLAS_TEMPORARY_DATABASE_USER_LIFETIME_MS` | 14400000 | Time in milliseconds that temporary database users created when connecting to MongoDB Atlas clusters will remain active before being automatically deleted. |
361362

362363
#### Logger Options
363364

@@ -418,6 +419,14 @@ Operation types:
418419
- `metadata` - Tools that read metadata, such as list databases, list collections, collection schema, etc.
419420
- `connect` - Tools that allow you to connect or switch the connection to a MongoDB instance. If this is disabled, you will need to provide a connection string through the config when starting the server.
420421

422+
#### Require Confirmation
423+
424+
If your client supports [elicitation](https://modelcontextprotocol.io/specification/draft/client/elicitation), you can set the MongoDB MCP server to request user confirmation before executing certain tools.
425+
426+
When a tool is marked as requiring confirmation, the server will send an elicitation request to the client. The client with elicitation support will then prompt the user for confirmation and send the response back to the server. If the client does not support elicitation, the tool will execute without confirmation.
427+
428+
You can set the `confirmationRequiredTools` configuration option to specify the names of tools which require confirmation. By default, the following tools have this setting enabled: `drop-database`, `drop-collection`, `delete-many`, `atlas-create-db-user`, `atlas-create-access-list`.
429+
421430
#### Read-Only Mode
422431

423432
The `readOnly` configuration option allows you to restrict the MCP server to only use tools with "read", "connect", and "metadata" operation types. When enabled, all tools that have "create", "update" or "delete" operation types will not be registered with the server.

src/common/config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ export interface UserConfig extends CliOptions {
160160
exportTimeoutMs: number;
161161
exportCleanupIntervalMs: number;
162162
connectionString?: string;
163+
// TODO: Use a type tracking all tool names.
163164
disabledTools: Array<string>;
165+
confirmationRequiredTools: Array<string>;
164166
readOnly?: boolean;
165167
indexCheck?: boolean;
166168
transport: "stdio" | "http";
@@ -183,6 +185,13 @@ export const defaultUserConfig: UserConfig = {
183185
telemetry: "enabled",
184186
readOnly: false,
185187
indexCheck: false,
188+
confirmationRequiredTools: [
189+
"atlas-create-access-list",
190+
"atlas-create-db-user",
191+
"drop-database",
192+
"drop-collection",
193+
"delete-many",
194+
],
186195
transport: "stdio",
187196
httpPort: 3000,
188197
httpHost: "127.0.0.1",
@@ -442,6 +451,7 @@ export function setupUserConfig({
442451

443452
userConfig.disabledTools = commaSeparatedToArray(userConfig.disabledTools);
444453
userConfig.loggers = commaSeparatedToArray(userConfig.loggers);
454+
userConfig.confirmationRequiredTools = commaSeparatedToArray(userConfig.confirmationRequiredTools);
445455

446456
if (userConfig.connectionString && userConfig.connectionSpecifier) {
447457
const connectionInfo = generateConnectionInfoFromCliArgs(userConfig);

src/elicitation.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type { ElicitRequest } from "@modelcontextprotocol/sdk/types.js";
2+
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
3+
4+
export class Elicitation {
5+
private readonly server: McpServer["server"];
6+
constructor({ server }: { server: McpServer["server"] }) {
7+
this.server = server;
8+
}
9+
10+
/**
11+
* Checks if the client supports elicitation capabilities.
12+
* @returns True if the client supports elicitation, false otherwise.
13+
*/
14+
public supportsElicitation(): boolean {
15+
const clientCapabilities = this.server.getClientCapabilities();
16+
return clientCapabilities?.elicitation !== undefined;
17+
}
18+
19+
/**
20+
* Requests a boolean confirmation from the user.
21+
* @param message - The message to display to the user.
22+
* @returns True if the user confirms the action or the client does not support elicitation, false otherwise.
23+
*/
24+
public async requestConfirmation(message: string): Promise<boolean> {
25+
if (!this.supportsElicitation()) {
26+
return true;
27+
}
28+
29+
const result = await this.server.elicitInput({
30+
message,
31+
requestedSchema: Elicitation.CONFIRMATION_SCHEMA,
32+
});
33+
return result.action === "accept" && result.content?.confirmation === "Yes";
34+
}
35+
36+
/**
37+
* The schema for the confirmation question.
38+
* TODO: In the future would be good to use Zod 4's toJSONSchema() to generate the schema.
39+
*/
40+
public static CONFIRMATION_SCHEMA: ElicitRequest["params"]["requestedSchema"] = {
41+
type: "object",
42+
properties: {
43+
confirmation: {
44+
type: "string",
45+
title: "Would you like to confirm?",
46+
description: "Would you like to confirm?",
47+
enum: ["Yes", "No"],
48+
enumNames: ["Yes, I confirm", "No, I do not confirm"],
49+
},
50+
},
51+
required: ["confirmation"],
52+
};
53+
}

0 commit comments

Comments
 (0)