Skip to content

Commit 79fe868

Browse files
chore: bring back static operationType
This commit brings back static operationType property on ToolClass declarations and ensures that current tools abides by it. Additionally we are now exporting Tools as a record instead of list to allow selective picking.
1 parent 9a6f1df commit 79fe868

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+909
-330
lines changed

MCP_SERVER_LIBRARY.md

Lines changed: 259 additions & 140 deletions
Large diffs are not rendered by default.

knip.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
"scripts/**/*.ts",
77
"eslint-rules/*.js"
88
],
9-
"ignore": ["tests/integration/fixtures/curl.mjs", "tests/vitest.d.ts"],
9+
"ignore": [
10+
// Knip, for some reason, is not able to link the exported tools to the
11+
// final exports in `tools/index.ts` and complains about unused exports. For
12+
// that reason we're ignoring the tool definitions.
13+
"src/tools/**/*.ts",
14+
"tests/integration/fixtures/curl.mjs",
15+
"tests/vitest.d.ts"
16+
],
1017
"ignoreDependencies": ["@mongodb-js/atlas-local"],
1118
"ignoreExportsUsedInFile": true
1219
}

src/lib.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
export { Server, type ServerOptions } from "./server.js";
22
export { Session, type SessionOptions } from "./common/session.js";
3-
export { type UserConfig } from "./common/config/userConfig.js";
3+
export { type UserConfig, UserConfigSchema } from "./common/config/userConfig.js";
4+
export { createUserConfig as parseCliArgumentsAsUserConfig } from "./common/config/createUserConfig.js";
45
export { LoggerBase, type LogPayload, type LoggerType, type LogLevel } from "./common/logger.js";
56
export { StreamableHttpRunner } from "./transports/streamableHttp.js";
67
export { StdioRunner } from "./transports/stdio.js";
78
export { TransportRunnerBase, type TransportRunnerConfig } from "./transports/base.js";
89
export {
910
ConnectionManager,
1011
ConnectionStateConnected,
12+
createMCPConnectionManager,
1113
type AnyConnectionState,
1214
type ConnectionState,
1315
type ConnectionStateDisconnected,
1416
type ConnectionStateErrored,
1517
type ConnectionManagerFactoryFn,
1618
} from "./common/connectionManager.js";
17-
export type {
18-
ConnectionErrorHandler,
19-
ConnectionErrorHandled,
20-
ConnectionErrorUnhandled,
21-
ConnectionErrorHandlerContext,
19+
export {
20+
connectionErrorHandler,
21+
type ConnectionErrorHandler,
22+
type ConnectionErrorHandled,
23+
type ConnectionErrorUnhandled,
24+
type ConnectionErrorHandlerContext,
2225
} from "./common/connectionErrorHandler.js";
23-
export { ErrorCodes } from "./common/errors.js";
26+
export { ErrorCodes, MongoDBError } from "./common/errors.js";
2427
export { Telemetry } from "./telemetry/telemetry.js";
2528
export { Keychain, registerGlobalSecretToRedact } from "./common/keychain.js";
2629
export type { Secret } from "./common/keychain.js";

src/server.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
UnsubscribeRequestSchema,
1717
} from "@modelcontextprotocol/sdk/types.js";
1818
import assert from "assert";
19-
import type { ToolBase, ToolCategory, ToolConstructorParams } from "./tools/tool.js";
19+
import type { ToolBase, ToolCategory, ToolClass } from "./tools/tool.js";
2020
import { validateConnectionString } from "./helpers/connectionOptions.js";
2121
import { packageInfo } from "./common/packageInfo.js";
2222
import { type ConnectionErrorHandler } from "./common/connectionErrorHandler.js";
@@ -35,10 +35,19 @@ export interface ServerOptions {
3535
* This will override any default tools. You can use both existing and custom tools by using the `mongodb-mcp-server/tools` export.
3636
*
3737
* ```ts
38-
* import { AllTools, ToolBase } from "mongodb-mcp-server/tools";
38+
* import { AllTools, ToolBase, type ToolCategory, type OperationType } from "mongodb-mcp-server/tools";
3939
* class CustomTool extends ToolBase {
40-
* name = "custom_tool";
41-
* // ...
40+
* override name = "custom_tool";
41+
* override category: ToolCategory = "mongodb";
42+
* static operationType: OperationType = "read";
43+
* protected description = "Custom tool description";
44+
* protected argsShape = {};
45+
* protected async execute() {
46+
* return { content: [{ type: "text", text: "Result" }] };
47+
* }
48+
* protected resolveTelemetryMetadata() {
49+
* return {};
50+
* }
4251
* }
4352
* const server = new Server({
4453
* session: mySession,
@@ -47,11 +56,11 @@ export interface ServerOptions {
4756
* telemetry: myTelemetry,
4857
* elicitation: myElicitation,
4958
* connectionErrorHandler: myConnectionErrorHandler,
50-
* tools: [...AllTools, CustomTool],
59+
* tools: [...Object.values(AllTools), CustomTool],
5160
* });
5261
* ```
5362
*/
54-
tools?: (new (params: ToolConstructorParams) => ToolBase)[];
63+
tools?: ToolClass[];
5564
}
5665

5766
export class Server {
@@ -60,7 +69,7 @@ export class Server {
6069
private readonly telemetry: Telemetry;
6170
public readonly userConfig: UserConfig;
6271
public readonly elicitation: Elicitation;
63-
private readonly toolConstructors: (new (params: ToolConstructorParams) => ToolBase)[];
72+
private readonly toolConstructors: ToolClass[];
6473
public readonly tools: ToolBase[] = [];
6574
public readonly connectionErrorHandler: ConnectionErrorHandler;
6675

@@ -89,7 +98,7 @@ export class Server {
8998
this.userConfig = userConfig;
9099
this.elicitation = elicitation;
91100
this.connectionErrorHandler = connectionErrorHandler;
92-
this.toolConstructors = tools ?? AllTools;
101+
this.toolConstructors = tools ?? Object.values(AllTools);
93102
}
94103

95104
async connect(transport: Transport): Promise<void> {
@@ -242,6 +251,7 @@ export class Server {
242251
private registerTools(): void {
243252
for (const toolConstructor of this.toolConstructors) {
244253
const tool = new toolConstructor({
254+
operationType: toolConstructor.operationType,
245255
session: this.session,
246256
config: this.userConfig,
247257
telemetry: this.telemetry,

src/tools/atlas/connect/connectCluster.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const ConnectClusterArgs = {
3232
export class ConnectClusterTool extends AtlasToolBase {
3333
public name = "atlas-connect-cluster";
3434
protected description = "Connect to MongoDB Atlas cluster";
35-
public operationType: OperationType = "connect";
35+
static operationType: OperationType = "connect";
3636
protected argsShape = {
3737
...ConnectClusterArgs,
3838
};

src/tools/atlas/create/createAccessList.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const CreateAccessListArgs = {
1919
export class CreateAccessListTool extends AtlasToolBase {
2020
public name = "atlas-create-access-list";
2121
protected description = "Allow Ip/CIDR ranges to access your MongoDB Atlas clusters.";
22-
public operationType: OperationType = "create";
22+
static operationType: OperationType = "create";
2323
protected argsShape = {
2424
...CreateAccessListArgs,
2525
};

src/tools/atlas/create/createDBUser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const CreateDBUserArgs = {
3636
export class CreateDBUserTool extends AtlasToolBase {
3737
public name = "atlas-create-db-user";
3838
protected description = "Create an MongoDB Atlas database user";
39-
public operationType: OperationType = "create";
39+
static operationType: OperationType = "create";
4040
protected argsShape = {
4141
...CreateDBUserArgs,
4242
};

src/tools/atlas/create/createFreeCluster.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { AtlasArgs } from "../../args.js";
88
export class CreateFreeClusterTool extends AtlasToolBase {
99
public name = "atlas-create-free-cluster";
1010
protected description = "Create a free MongoDB Atlas cluster";
11-
public operationType: OperationType = "create";
11+
static operationType: OperationType = "create";
1212
protected argsShape = {
1313
projectId: AtlasArgs.projectId().describe("Atlas project ID to create the cluster in"),
1414
name: AtlasArgs.clusterName().describe("Name of the cluster"),

src/tools/atlas/create/createProject.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { AtlasArgs } from "../../args.js";
77
export class CreateProjectTool extends AtlasToolBase {
88
public name = "atlas-create-project";
99
protected description = "Create a MongoDB Atlas project";
10-
public operationType: OperationType = "create";
10+
static operationType: OperationType = "create";
1111
protected argsShape = {
1212
projectName: AtlasArgs.projectName().optional().describe("Name for the new project"),
1313
organizationId: AtlasArgs.organizationId().optional().describe("Organization ID for the new project"),

src/tools/atlas/read/getPerformanceAdvisor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const PerformanceAdvisorOperationType = z.enum([
2626
export class GetPerformanceAdvisorTool extends AtlasToolBase {
2727
public name = "atlas-get-performance-advisor";
2828
protected description = `Get MongoDB Atlas performance advisor recommendations, which includes the operations: suggested indexes, drop index suggestions, schema suggestions, and a sample of the most recent (max ${DEFAULT_SLOW_QUERY_LOGS_LIMIT}) slow query logs`;
29-
public operationType: OperationType = "read";
29+
static operationType: OperationType = "read";
3030
protected argsShape = {
3131
projectId: AtlasArgs.projectId().describe(
3232
"Atlas project ID to get performance advisor recommendations. The project ID is a hexadecimal identifier of 24 characters. If the user has only specified the name, use the `atlas-list-projects` tool to retrieve the user's projects with their ids."

0 commit comments

Comments
 (0)