Skip to content
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

feat: Add resource handling to MCP server #1609

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/mcp/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import {
CallToolRequestSchema,
ListResourcesRequestSchema,
ListToolsRequestSchema,
ReadResourceRequestSchema,
Resource,
Tool,
ToolSchema,
} from "@modelcontextprotocol/sdk/types.js";
Expand All @@ -18,6 +21,7 @@ import {
} from "@extension";
import { RunModelParams } from "../dbt_client/dbtIntegration";
import { CommandProcessResult } from "../commandProcessExecution";
import { existsSync, readFileSync } from "fs";

const ToolInputSchema = ToolSchema.shape.inputSchema;
type ToolInput = z.infer<typeof ToolInputSchema>;
Expand Down Expand Up @@ -152,6 +156,68 @@ export class DbtPowerUserMcpServerTools implements Disposable {
this.dbtTerminal.error("DbtPowerUserMcpServerTools", "Error", { error });
};

const getProjectResources = () => {
return this.dbtProjectContainer.getProjects().flatMap((project) => {
const projectRoot = project.projectRoot.fsPath;
const resources: Resource[] = [];

// Add manifest if exists
const manifestPath = project.getManifestPath();
if (manifestPath && existsSync(manifestPath)) {
resources.push({
uri: `file://${encodeURI(manifestPath)}`,
name: "manifest.json",
description: `dbt manifest for ${project.getProjectName()}`,
mimeType: "application/json",
});
}

// Add catalog if exists
const catalogPath = project.getCatalogPath();
if (catalogPath && existsSync(catalogPath)) {
resources.push({
uri: `file://${encodeURI(catalogPath)}`,
name: "catalog.json",
description: `dbt catalog for ${project.getProjectName()}`,
mimeType: "application/json",
});
}

return resources;
});
};

// List available resources
server.setRequestHandler(ListResourcesRequestSchema, async () => {
return { resources: getProjectResources() };
});

// Read resource contents
server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
const decodedUri = decodeURIComponent(request.params.uri);
const filePath = Uri.parse(decodedUri).fsPath;

// Security: Validate path belongs to a known project
const isValid = this.dbtProjectContainer.getProjects().some((project) => {
return project.contains(Uri.file(filePath));
});

if (!existsSync(filePath) || !isValid) {
throw new Error("Resource not found or unauthorized");
}

return {
contents: [
{
uri: request.params.uri,
mimeType: "application/json",
text: readFileSync(filePath, "utf8"),
},
],
};
});

// Existing tools handler continues below
server.setRequestHandler(ListToolsRequestSchema, async () => {
this.dbtTerminal.debug("DbtPowerUserMcpServerTools", "Listing tools");
const tools: Tool[] = [
Expand Down
Loading