Skip to content

Commit f3c3064

Browse files
chore: add developer guide and code examples to extend MCP server via library exports MCP-299 (#767)
1 parent 0e7d241 commit f3c3064

Some content is hidden

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

58 files changed

+1756
-195
lines changed

MCP_SERVER_LIBRARY.md

Lines changed: 1068 additions & 0 deletions
Large diffs are not rendered by default.

knip.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"entry": [
33
"src/index.ts!",
44
"src/lib.ts!",
5+
"src/tools/index.ts!",
56
"tests/**/*.ts",
67
"scripts/**/*.ts",
78
"eslint-rules/*.js"

src/lib.ts

Lines changed: 9 additions & 6 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";
33
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: 17 additions & 6 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+
* static 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,
@@ -51,7 +60,7 @@ export interface ServerOptions {
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

@@ -242,6 +251,8 @@ export class Server {
242251
private registerTools(): void {
243252
for (const toolConstructor of this.toolConstructors) {
244253
const tool = new toolConstructor({
254+
category: toolConstructor.category,
255+
operationType: toolConstructor.operationType,
245256
session: this.session,
246257
config: this.userConfig,
247258
telemetry: this.telemetry,

src/tools/atlas/atlasTool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { z } from "zod";
77
import { ApiClientError } from "../../common/atlas/apiClientError.js";
88

99
export abstract class AtlasToolBase extends ToolBase {
10-
public category: ToolCategory = "atlas";
10+
static category: ToolCategory = "atlas";
1111

1212
protected verifyAllowed(): boolean {
1313
if (!this.config.apiClientId || !this.config.apiClientSecret) {

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"),

0 commit comments

Comments
 (0)