Skip to content

Commit 21d858a

Browse files
committed
fix: resolve resource paths in packaged extension
1 parent 13a2e1b commit 21d858a

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

packages/ansible-mcp-server/src/resources/agents.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,27 @@ import fs from "node:fs/promises";
55
import path from "node:path";
66
import { fileURLToPath } from "node:url";
77

8-
const __filename = fileURLToPath(import.meta.url);
9-
const __dirname = path.dirname(__filename);
8+
// Get the directory where resources are located
9+
// In bundled output (out/mcp/cli.js), resources are at out/mcp/data/
10+
// In source/dev mode, resources are at src/resources/data/
11+
function getResourceDir(): string {
12+
// In bundled mode, process.argv[1] points to the entry script (out/mcp/cli.js)
13+
// This is the most reliable way to get the directory in bundled code
14+
// since import.meta.url doesn't resolve correctly when webpack bundles ESM to CommonJS
15+
if (typeof process !== "undefined" && process.argv && process.argv[1]) {
16+
return path.dirname(process.argv[1]);
17+
}
18+
// Fall back to import.meta.url (works in dev mode when running with ts-node)
19+
const __filename = fileURLToPath(import.meta.url);
20+
return path.dirname(__filename);
21+
}
22+
23+
const resourceDir = getResourceDir();
1024

1125
// Agents guidelines file packaged with the extension
12-
// In compiled output, files are in out/server/src/resources/data/
26+
// In compiled output, files are in out/mcp/data/
1327
// In source, files are in src/resources/data/
14-
const AGENTS_FILE = path.join(__dirname, "data/agents.md");
28+
const AGENTS_FILE = path.join(resourceDir, "data/agents.md");
1529

1630
/**
1731
* Get the agents.md file content from the packaged file.

packages/ansible-mcp-server/src/resources/eeSchema.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,34 @@ import fs from "node:fs/promises";
55
import path from "node:path";
66
import { fileURLToPath } from "node:url";
77

8-
const __filename = fileURLToPath(import.meta.url);
9-
const __dirname = path.dirname(__filename);
8+
// Get the directory where resources are located
9+
// In bundled output (out/mcp/cli.js), resources are at out/mcp/data/
10+
// In source/dev mode, resources are at src/resources/data/
11+
function getResourceDir(): string {
12+
// In bundled mode, process.argv[1] points to the entry script (out/mcp/cli.js)
13+
// This is the most reliable way to get the directory in bundled code
14+
// since import.meta.url doesn't resolve correctly when webpack bundles ESM to CommonJS
15+
if (typeof process !== "undefined" && process.argv && process.argv[1]) {
16+
return path.dirname(process.argv[1]);
17+
}
18+
// Fall back to import.meta.url (works in dev mode when running with ts-node)
19+
const __filename = fileURLToPath(import.meta.url);
20+
return path.dirname(__filename);
21+
}
22+
23+
const resourceDir = getResourceDir();
1024

1125
// Schema file packaged with the extension
12-
// In compiled output, files are in out/server/src/resources/data/
26+
// In compiled output, files are in out/mcp/data/
1327
// In source, files are in src/resources/data/
1428
const SCHEMA_FILE = path.join(
15-
__dirname,
29+
resourceDir,
1630
"data/execution-environment-schema.json",
1731
);
18-
const RULES_FILE = path.join(__dirname, "data/ee-rules.md");
32+
const RULES_FILE = path.join(resourceDir, "data/ee-rules.md");
1933
// Sample EE file packaged with the extension
2034
export const SAMPLE_EE_FILE = path.join(
21-
__dirname,
35+
resourceDir,
2236
"data/execution-environment-sample.yml",
2337
);
2438

0 commit comments

Comments
 (0)