-
Notifications
You must be signed in to change notification settings - Fork 41
feat: add createCollection tool #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; | ||
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js"; | ||
import { OperationType, ToolArgs } from "../../tool.js"; | ||
|
||
export class CreateCollectionTool extends MongoDBToolBase { | ||
protected name = "create-collection"; | ||
protected description = | ||
"Creates a new collection in a database. If the database doesn't exist, it will be created automatically."; | ||
protected argsShape = DbOperationArgs; | ||
|
||
protected operationType: OperationType = "create"; | ||
|
||
protected async execute({ collection, database }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> { | ||
const provider = await this.ensureConnected(); | ||
await provider.createCollection(database, collection); | ||
|
||
return { | ||
content: [ | ||
{ | ||
type: "text", | ||
text: `Collection "${collection}" created in database "${database}".`, | ||
}, | ||
], | ||
}; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
tests/integration/tools/mongodb/create/createCollection.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { | ||
connect, | ||
jestTestCluster, | ||
jestTestMCPClient, | ||
getResponseContent, | ||
validateParameters, | ||
} from "../../../helpers.js"; | ||
import { toIncludeSameMembers } from "jest-extended"; | ||
import { McpError } from "@modelcontextprotocol/sdk/types.js"; | ||
import { ObjectId } from "bson"; | ||
|
||
describe("createCollection tool", () => { | ||
const client = jestTestMCPClient(); | ||
const cluster = jestTestCluster(); | ||
|
||
it("should have correct metadata", async () => { | ||
const { tools } = await client().listTools(); | ||
const listCollections = tools.find((tool) => tool.name === "create-collection")!; | ||
expect(listCollections).toBeDefined(); | ||
expect(listCollections.description).toBe( | ||
"Creates a new collection in a database. If the database doesn't exist, it will be created automatically." | ||
); | ||
|
||
validateParameters(listCollections, [ | ||
{ | ||
name: "database", | ||
description: "Database name", | ||
type: "string", | ||
}, | ||
{ | ||
name: "collection", | ||
description: "Collection name", | ||
type: "string", | ||
}, | ||
]); | ||
}); | ||
|
||
describe("with invalid arguments", () => { | ||
const args = [ | ||
{}, | ||
{ database: 123, collection: "bar" }, | ||
{ foo: "bar", database: "test", collection: "bar" }, | ||
{ collection: [], database: "test" }, | ||
]; | ||
for (const arg of args) { | ||
it(`throws a schema error for: ${JSON.stringify(arg)}`, async () => { | ||
await connect(client(), cluster()); | ||
try { | ||
await client().callTool({ name: "create-collection", arguments: arg }); | ||
expect.fail("Expected an error to be thrown"); | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(McpError); | ||
const mcpError = error as McpError; | ||
expect(mcpError.code).toEqual(-32602); | ||
expect(mcpError.message).toContain("Invalid arguments for tool create-collection"); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
describe("with non-existent database", () => { | ||
it("creates a new collection", async () => { | ||
const mongoClient = cluster().getClient(); | ||
let collections = await mongoClient.db("foo").listCollections().toArray(); | ||
expect(collections).toHaveLength(0); | ||
|
||
await connect(client(), cluster()); | ||
const response = await client().callTool({ | ||
name: "create-collection", | ||
arguments: { database: "foo", collection: "bar" }, | ||
}); | ||
const content = getResponseContent(response.content); | ||
expect(content).toEqual('Collection "bar" created in database "foo".'); | ||
|
||
collections = await mongoClient.db("foo").listCollections().toArray(); | ||
expect(collections).toHaveLength(1); | ||
expect(collections[0].name).toEqual("bar"); | ||
}); | ||
}); | ||
|
||
describe("with existing database", () => { | ||
let dbName: string; | ||
beforeEach(() => { | ||
dbName = new ObjectId().toString(); | ||
}); | ||
|
||
it("creates new collection", async () => { | ||
const mongoClient = cluster().getClient(); | ||
await mongoClient.db(dbName).createCollection("collection1"); | ||
let collections = await mongoClient.db(dbName).listCollections().toArray(); | ||
expect(collections).toHaveLength(1); | ||
|
||
await connect(client(), cluster()); | ||
const response = await client().callTool({ | ||
name: "create-collection", | ||
arguments: { database: dbName, collection: "collection2" }, | ||
}); | ||
const content = getResponseContent(response.content); | ||
expect(content).toEqual(`Collection "collection2" created in database "${dbName}".`); | ||
collections = await mongoClient.db(dbName).listCollections().toArray(); | ||
expect(collections).toHaveLength(2); | ||
expect(collections.map((c) => c.name)).toIncludeSameMembers(["collection1", "collection2"]); | ||
}); | ||
|
||
it("does nothing if collection already exists", async () => { | ||
const mongoClient = cluster().getClient(); | ||
await mongoClient.db(dbName).collection("collection1").insertOne({}); | ||
let collections = await mongoClient.db(dbName).listCollections().toArray(); | ||
expect(collections).toHaveLength(1); | ||
let documents = await mongoClient.db(dbName).collection("collection1").find({}).toArray(); | ||
expect(documents).toHaveLength(1); | ||
|
||
await connect(client(), cluster()); | ||
const response = await client().callTool({ | ||
name: "create-collection", | ||
arguments: { database: dbName, collection: "collection1" }, | ||
}); | ||
const content = getResponseContent(response.content); | ||
expect(content).toEqual(`Collection "collection1" created in database "${dbName}".`); | ||
collections = await mongoClient.db(dbName).listCollections().toArray(); | ||
expect(collections).toHaveLength(1); | ||
expect(collections[0].name).toEqual("collection1"); | ||
|
||
// Make sure we didn't drop the existing collection | ||
documents = await mongoClient.db(dbName).collection("collection1").find({}).toArray(); | ||
expect(documents).toHaveLength(1); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
tests/integration/tools/mongodb/metadata/listCollections.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { | ||
getResponseElements, | ||
connect, | ||
jestTestCluster, | ||
jestTestMCPClient, | ||
getResponseContent, | ||
getParameters, | ||
validateParameters, | ||
} from "../../../helpers.js"; | ||
import { toIncludeSameMembers } from "jest-extended"; | ||
import { McpError } from "@modelcontextprotocol/sdk/types.js"; | ||
|
||
describe("listCollections tool", () => { | ||
const client = jestTestMCPClient(); | ||
const cluster = jestTestCluster(); | ||
|
||
it("should have correct metadata", async () => { | ||
const { tools } = await client().listTools(); | ||
const listCollections = tools.find((tool) => tool.name === "list-collections")!; | ||
expect(listCollections).toBeDefined(); | ||
expect(listCollections.description).toBe("List all collections for a given database"); | ||
|
||
validateParameters(listCollections, [{ name: "database", description: "Database name", type: "string" }]); | ||
}); | ||
|
||
describe("with invalid arguments", () => { | ||
const args = [{}, { database: 123 }, { foo: "bar", database: "test" }, { database: [] }]; | ||
for (const arg of args) { | ||
it(`throws a schema error for: ${JSON.stringify(arg)}`, async () => { | ||
await connect(client(), cluster()); | ||
try { | ||
await client().callTool({ name: "list-collections", arguments: arg }); | ||
expect.fail("Expected an error to be thrown"); | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(McpError); | ||
const mcpError = error as McpError; | ||
expect(mcpError.code).toEqual(-32602); | ||
expect(mcpError.message).toContain("Invalid arguments for tool list-collections"); | ||
expect(mcpError.message).toContain('"expected": "string"'); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
describe("with non-existent database", () => { | ||
it("returns no collections", async () => { | ||
await connect(client(), cluster()); | ||
const response = await client().callTool({ | ||
name: "list-collections", | ||
arguments: { database: "non-existent" }, | ||
}); | ||
const content = getResponseContent(response.content); | ||
expect(content).toEqual( | ||
`No collections found for database "non-existent". To create a collection, use the "create-collection" tool.` | ||
); | ||
}); | ||
}); | ||
|
||
describe("with existing database", () => { | ||
it("returns collections", async () => { | ||
const mongoClient = cluster().getClient(); | ||
await mongoClient.db("my-db").createCollection("collection-1"); | ||
|
||
await connect(client(), cluster()); | ||
const response = await client().callTool({ | ||
name: "list-collections", | ||
arguments: { database: "my-db" }, | ||
}); | ||
const items = getResponseElements(response.content); | ||
expect(items).toHaveLength(1); | ||
expect(items[0].text).toContain('Name: "collection-1"'); | ||
|
||
await mongoClient.db("my-db").createCollection("collection-2"); | ||
|
||
const response2 = await client().callTool({ | ||
name: "list-collections", | ||
arguments: { database: "my-db" }, | ||
}); | ||
const items2 = getResponseElements(response2.content); | ||
expect(items2).toHaveLength(2); | ||
expect(items2.map((item) => item.text)).toIncludeSameMembers([ | ||
'Name: "collection-1"', | ||
'Name: "collection-2"', | ||
]); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not blocking: this isn't from this PR but naming feels weird in retrospect as jest is an implementation detail. maybe
mock
or justgetTestMCPClient
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, it does also call the before/after hooks, so it kind of depends on jest (or a similar testing framework that has the same hooks with similar signatures). Also, I don't think it'd be accurate to call it a mock client as it's more of an integration setup. Happy to iterate on the naming though if we feel this is not the right one.