Skip to content

Commit a9b5685

Browse files
committed
provide the state in the ctor
1 parent e210323 commit a9b5685

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

src/tools/atlas/atlasTool.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { ZodRawShape } from "zod";
22
import { ToolBase } from "../tool.js";
33
import { ApiClient } from "../../client.js";
4+
import { State } from "../../state.js";
45

56
export abstract class AtlasToolBase<Args extends ZodRawShape = ZodRawShape> extends ToolBase<Args> {
6-
constructor(protected apiClient: ApiClient) {
7-
super();
7+
constructor(
8+
state: State,
9+
protected apiClient: ApiClient
10+
) {
11+
super(state);
812
}
913
}

src/tools/atlas/listClusters.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { config } from "../../config.js";
44
import { ensureAuthenticated } from "./auth.js";
55
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
66
import { AtlasToolBase } from "./atlasTool.js";
7+
import { State } from "../../state.js";
78

89
export class ListClustersTool extends AtlasToolBase<{
910
projectId: ZodString | ZodOptional<ZodString>;
@@ -12,8 +13,8 @@ export class ListClustersTool extends AtlasToolBase<{
1213
protected description = "List MongoDB Atlas clusters";
1314
protected argsShape;
1415

15-
constructor(apiClient: ApiClient) {
16-
super(apiClient);
16+
constructor(state: State, apiClient: ApiClient) {
17+
super(state, apiClient);
1718

1819
let projectIdFilter: ZodString | ZodOptional<ZodString> = z
1920
.string()

src/tools/atlas/tools.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import { ListProjectsTool } from "./listProjects.js";
99

1010
export function registerAtlasTools(server: McpServer, state: State, apiClient: ApiClient) {
1111
const tools: ToolBase<ZodRawShape>[] = [
12-
new AuthTool(apiClient),
13-
new ListClustersTool(apiClient),
14-
new ListProjectsTool(apiClient),
12+
new AuthTool(state, apiClient),
13+
new ListClustersTool(state, apiClient),
14+
new ListProjectsTool(state, apiClient),
1515
];
1616

1717
for (const tool of tools) {
18-
tool.register(server, state);
18+
tool.register(server);
1919
}
2020
}

src/tools/tool.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
55
import { State } from "../state.js";
66

77
export abstract class ToolBase<Args extends ZodRawShape> {
8-
protected state: State = undefined!; // We should never use this before it's set
98
protected abstract name: string;
109

1110
protected abstract description: string;
@@ -14,9 +13,9 @@ export abstract class ToolBase<Args extends ZodRawShape> {
1413

1514
protected abstract execute(args: z.objectOutputType<Args, ZodTypeAny>): Promise<CallToolResult>;
1615

17-
public register(server: McpServer, state: State): void {
18-
this.state = state;
16+
protected constructor(protected state: State) {}
1917

18+
public register(server: McpServer): void {
2019
const callback = async (args: z.objectOutputType<Args, ZodTypeAny>): Promise<CallToolResult> => {
2120
try {
2221
// TODO: add telemetry here

0 commit comments

Comments
 (0)