Skip to content

Commit 272e269

Browse files
committed
feat: add GitHub error handling to MCP server
- Import GitHubError types - Add error formatting utility - Update error handling in request handler
1 parent 10bd24d commit 272e269

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/github/index.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ import * as pulls from './operations/pulls.js';
1515
import * as branches from './operations/branches.js';
1616
import * as search from './operations/search.js';
1717
import * as commits from './operations/commits.js';
18+
import {
19+
GitHubError,
20+
GitHubValidationError,
21+
GitHubResourceNotFoundError,
22+
GitHubAuthenticationError,
23+
GitHubPermissionError,
24+
GitHubRateLimitError,
25+
GitHubConflictError,
26+
isGitHubError,
27+
} from './common/errors.js';
1828

1929
const server = new Server(
2030
{
@@ -28,6 +38,29 @@ const server = new Server(
2838
}
2939
);
3040

41+
function formatGitHubError(error: GitHubError): string {
42+
let message = `GitHub API Error: ${error.message}`;
43+
44+
if (error instanceof GitHubValidationError) {
45+
message = `Validation Error: ${error.message}`;
46+
if (error.response) {
47+
message += `\nDetails: ${JSON.stringify(error.response)}`;
48+
}
49+
} else if (error instanceof GitHubResourceNotFoundError) {
50+
message = `Not Found: ${error.message}`;
51+
} else if (error instanceof GitHubAuthenticationError) {
52+
message = `Authentication Failed: ${error.message}`;
53+
} else if (error instanceof GitHubPermissionError) {
54+
message = `Permission Denied: ${error.message}`;
55+
} else if (error instanceof GitHubRateLimitError) {
56+
message = `Rate Limit Exceeded: ${error.message}\nResets at: ${error.resetAt.toISOString()}`;
57+
} else if (error instanceof GitHubConflictError) {
58+
message = `Conflict: ${error.message}`;
59+
}
60+
61+
return message;
62+
}
63+
3164
server.setRequestHandler(ListToolsRequestSchema, async () => {
3265
return {
3366
tools: [
@@ -298,7 +331,10 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
298331
}
299332
} catch (error) {
300333
if (error instanceof z.ZodError) {
301-
throw new Error(`ZodErrors: ${JSON.stringify(error.errors)}`);
334+
throw new Error(`Invalid input: ${JSON.stringify(error.errors)}`);
335+
}
336+
if (isGitHubError(error)) {
337+
throw new Error(formatGitHubError(error));
302338
}
303339
throw error;
304340
}

0 commit comments

Comments
 (0)