lance-mcp
Version:
MCP server for interacting with LanceDB database
68 lines (67 loc) • 2.43 kB
JavaScript
import { ErrorCode, McpError } from "@modelcontextprotocol/sdk/types.js";
import { client } from "../../mongodb/client.js";
import { BaseTool } from "../base/tool.js";
export class AggregateTool extends BaseTool {
constructor() {
super(...arguments);
this.name = "aggregate";
this.description = "Execute a MongoDB aggregation pipeline";
this.inputSchema = {
type: "object",
properties: {
database: {
type: "string",
description: "Name of the database to use",
},
collection: {
type: "string",
description: "Name of the collection to query",
},
pipeline: {
type: "string",
description: "Aggregation pipeline stages",
default: {},
},
},
required: ["database", "collection", "pipeline"],
};
}
parsePipeline(pipeline) {
try {
const parsedPipeline = JSON.parse(pipeline);
if (!Array.isArray(parsedPipeline)) {
throw new McpError(ErrorCode.InvalidRequest, `Parsed pipeline must be an array, got ${typeof parsedPipeline}`);
}
return parsedPipeline;
}
catch (error) {
throw new McpError(ErrorCode.InvalidRequest, `Failed to parse pipeline: ${error.message}`);
}
}
validatePipeline(pipeline) {
if (!Array.isArray(pipeline)) {
throw new McpError(ErrorCode.InvalidRequest, `Pipeline must be an array, got ${typeof pipeline}`);
}
return pipeline;
}
async execute(params) {
try {
const database = this.validateDatabase(params.database);
const collection = this.validateCollection(params.collection);
const pipeline = this.parsePipeline(params.pipeline);
const results = await client
.db(database)
.collection(collection)
.aggregate(pipeline).toArray();
return {
content: [
{ type: "text", text: JSON.stringify(results, null, 2) },
],
isError: false,
};
}
catch (error) {
return this.handleError(error);
}
}
}