Skip to content

Commit 243cbf2

Browse files
Copilotdata-douser
andcommitted
Add path resolution and error handling for prompt file path parameters
- Add resolvePromptFilePath() utility that resolves relative paths against workspace root and returns user-friendly warnings for invalid paths - Update all 9 prompt handlers with file path parameters to resolve paths and embed warnings in the response instead of throwing protocol errors - Add integration test in mcp-test-suite.js for explain_codeql_query with invalid path - Add file-based integration test fixtures in primitives/prompts/ - Add 10 unit tests for resolvePromptFilePath and handler path resolution Co-authored-by: data-douser <70299490+data-douser@users.noreply.github.com>
1 parent 425c19c commit 243cbf2

File tree

8 files changed

+681
-188
lines changed

8 files changed

+681
-188
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Integration Test: explain_codeql_query / invalid_query_path
2+
3+
## Purpose
4+
5+
Verify that the `explain_codeql_query` prompt returns a helpful error message
6+
when the user provides a `queryPath` that does not exist on disk.
7+
8+
## Expected Behavior
9+
10+
- The prompt handler resolves the relative path against the workspace root.
11+
- When the resolved path does not exist, the prompt returns a warning in the
12+
response messages rather than throwing a raw MCP protocol error.
13+
- The warning message should mention the file was not found so the user can
14+
correct the path.
15+
16+
## Parameters
17+
18+
| Parameter | Value | Notes |
19+
| ----------- | ------------------------------ | ------------------ |
20+
| `queryPath` | `nonexistent/path/to/query.ql` | Does **not** exist |
21+
| `language` | `javascript` | Valid language |
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"sessions": []
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"sessions": []
3+
}

client/src/lib/mcp-test-suite.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export class MCPTestSuite {
2222
await this.testReadResource();
2323
await this.testListPrompts();
2424
await this.testGetPrompt();
25+
await this.testGetPromptWithInvalidPath();
2526
}
2627

2728
/**
@@ -132,6 +133,58 @@ export class MCPTestSuite {
132133
}
133134
}
134135

136+
/**
137+
* Test that prompts handle invalid file paths gracefully.
138+
*
139+
* The explain_codeql_query prompt requires a `queryPath` parameter.
140+
* When given a nonexistent path it should return a user-friendly error
141+
* message inside the prompt response rather than throwing a raw MCP
142+
* protocol error.
143+
*/
144+
async testGetPromptWithInvalidPath() {
145+
try {
146+
this.logger.log("Testing prompt error handling with invalid file path...");
147+
148+
const result = await this.client.getPrompt({
149+
name: "explain_codeql_query",
150+
arguments: {
151+
queryPath: "nonexistent/path/to/query.ql",
152+
language: "javascript"
153+
}
154+
});
155+
156+
// The prompt should return messages (not throw) even for an invalid path
157+
const hasMessages = result.messages && result.messages.length > 0;
158+
if (!hasMessages) {
159+
this.logger.logTest(
160+
"Get Prompt with Invalid Path (explain_codeql_query)",
161+
false,
162+
"Expected messages in response"
163+
);
164+
return false;
165+
}
166+
167+
// The response should contain a human-readable error about the invalid path
168+
const text = result.messages[0]?.content?.text || "";
169+
const mentionsPathError =
170+
text.includes("does not exist") ||
171+
text.includes("not found") ||
172+
text.includes("could not be found") ||
173+
text.includes("File not found") ||
174+
text.includes("⚠");
175+
176+
this.logger.log(`Prompt response mentions path error: ${mentionsPathError}`);
177+
this.logger.logTest("Get Prompt with Invalid Path (explain_codeql_query)", mentionsPathError);
178+
179+
return mentionsPathError;
180+
} catch (error) {
181+
// If the server throws instead of returning a message this test fails,
182+
// which is the exact behaviour we want to fix.
183+
this.logger.logTest("Get Prompt with Invalid Path (explain_codeql_query)", false, error);
184+
return false;
185+
}
186+
}
187+
135188
/**
136189
* Test listing prompts
137190
*/

0 commit comments

Comments
 (0)