@@ -6,90 +6,156 @@ import { Octokit } from "@octokit/rest";
66import { type } from "arktype" ;
77import { z } from "zod" ;
88import { resolveRepoContext } from "../utils/repo-context.ts" ;
9+ import { writeFileSync } from "node:fs" ;
910
10- const server = new McpServer ( {
11- name : "Minimal GitHub Issue Comment Server" ,
12- version : "0.0.1" ,
13- } ) ;
11+ // Simple error logging to file
12+ function logError ( message : string , error ?: any ) {
13+ const timestamp = new Date ( ) . toISOString ( ) ;
14+ const errorText = error
15+ ? `\nError: ${ error . message } \nStack: ${ error . stack } `
16+ : "" ;
17+ const logEntry = `[${ timestamp } ] ${ message } ${ errorText } \n` ;
18+
19+ try {
20+ writeFileSync ( "/tmp/mcp-error.log" , logEntry , { flag : "a" } ) ;
21+ console . error ( logEntry ) ;
22+ } catch ( writeError ) {
23+ console . error ( `Failed to write error log: ${ writeError } ` ) ;
24+ console . error ( logEntry ) ;
25+ }
26+ }
27+
28+ let server : McpServer ;
29+
30+ try {
31+ logError ( "Creating MCP server..." ) ;
32+ server = new McpServer ( {
33+ name : "Minimal GitHub Issue Comment Server" ,
34+ version : "0.0.1" ,
35+ } ) ;
36+ logError ( "MCP server created successfully" ) ;
37+ } catch ( error ) {
38+ logError ( "Failed to create MCP server" , error ) ;
39+ process . exit ( 1 ) ;
40+ }
1441
1542// Define the schema for creating issue comments
1643const Comment = type ( {
1744 issueNumber : type . number . describe ( "the issue number to comment on" ) ,
1845 body : type . string . describe ( "the comment body content" ) ,
1946} ) ;
2047
21- server . tool (
22- "create_issue_comment" ,
23- "Create a comment on a GitHub issue" ,
24- {
25- issueNumber : z . number ( ) . describe ( "the issue number to comment on" ) ,
26- body : z . string ( ) . describe ( "the comment body content" ) ,
27- } ,
28- async ( { issueNumber, body } ) => {
29- try {
30- Comment . assert ( { issueNumber, body } ) ;
31-
32- const githubInstallationToken = process . env . GITHUB_INSTALLATION_TOKEN ;
33- if ( ! githubInstallationToken ) {
34- throw new Error (
35- "GITHUB_INSTALLATION_TOKEN environment variable is required"
36- ) ;
37- }
48+ try {
49+ logError ( "Registering create_issue_comment tool..." ) ;
50+
51+ server . tool (
52+ "create_issue_comment" ,
53+ "Create a comment on a GitHub issue" ,
54+ {
55+ issueNumber : z . number ( ) . describe ( "the issue number to comment on" ) ,
56+ body : z . string ( ) . describe ( "the comment body content" ) ,
57+ } ,
58+ async ( { issueNumber, body } ) => {
59+ try {
60+ Comment . assert ( { issueNumber, body } ) ;
61+
62+ const githubInstallationToken = process . env . GITHUB_INSTALLATION_TOKEN ;
63+ if ( ! githubInstallationToken ) {
64+ throw new Error (
65+ "GITHUB_INSTALLATION_TOKEN environment variable is required"
66+ ) ;
67+ }
68+
69+ // Resolve repository context from environment
70+ const repoContext = resolveRepoContext ( ) ;
71+
72+ const octokit = new Octokit ( {
73+ auth : githubInstallationToken ,
74+ } ) ;
75+
76+ const result = await octokit . rest . issues . createComment ( {
77+ owner : repoContext . owner ,
78+ repo : repoContext . name ,
79+ issue_number : issueNumber ,
80+ body : body ,
81+ } ) ;
3882
39- // Resolve repository context from environment
40- const repoContext = resolveRepoContext ( ) ;
41-
42- const octokit = new Octokit ( {
43- auth : githubInstallationToken ,
44- } ) ;
45-
46- const result = await octokit . rest . issues . createComment ( {
47- owner : repoContext . owner ,
48- repo : repoContext . name ,
49- issue_number : issueNumber ,
50- body : body ,
51- } ) ;
52-
53- return {
54- content : [
55- {
56- type : "text" ,
57- text : JSON . stringify (
58- {
59- success : true ,
60- commentId : result . data . id ,
61- url : result . data . html_url ,
62- body : result . data . body ,
63- } ,
64- null ,
65- 2
66- ) ,
67- } ,
68- ] ,
69- } ;
70- } catch ( error ) {
71- const errorMessage =
72- error instanceof Error ? error . message : String ( error ) ;
73- return {
74- content : [
75- {
76- type : "text" ,
77- text : `Error creating comment: ${ errorMessage } ` ,
78- } ,
79- ] ,
80- error : errorMessage ,
81- isError : true ,
82- } ;
83+ return {
84+ content : [
85+ {
86+ type : "text" ,
87+ text : JSON . stringify (
88+ {
89+ success : true ,
90+ commentId : result . data . id ,
91+ url : result . data . html_url ,
92+ body : result . data . body ,
93+ } ,
94+ null ,
95+ 2
96+ ) ,
97+ } ,
98+ ] ,
99+ } ;
100+ } catch ( error ) {
101+ const errorMessage =
102+ error instanceof Error ? error . message : String ( error ) ;
103+ logError ( "Tool execution failed" , error ) ;
104+ return {
105+ content : [
106+ {
107+ type : "text" ,
108+ text : `Error creating comment: ${ errorMessage } ` ,
109+ } ,
110+ ] ,
111+ error : errorMessage ,
112+ isError : true ,
113+ } ;
114+ }
83115 }
84- }
85- ) ;
116+ ) ;
117+
118+ logError ( "Tool registered successfully" ) ;
119+ } catch ( error ) {
120+ logError ( "Failed to register tool" , error ) ;
121+ process . exit ( 1 ) ;
122+ }
86123
87124async function runServer ( ) {
88- const transport = new StdioServerTransport ( ) ;
89- await server . connect ( transport ) ;
90- process . on ( "exit" , ( ) => {
91- server . close ( ) ;
92- } ) ;
125+ try {
126+ logError ( "Starting MCP server..." ) ;
127+
128+ const transport = new StdioServerTransport ( ) ;
129+ logError ( "Transport created, attempting connection..." ) ;
130+
131+ await server . connect ( transport ) ;
132+ logError ( "MCP server connected successfully" ) ;
133+
134+ process . on ( "exit" , ( ) => {
135+ logError ( "Process exiting, closing server..." ) ;
136+ server . close ( ) ;
137+ } ) ;
138+
139+ process . on ( "SIGTERM" , ( ) => {
140+ logError ( "SIGTERM received, closing server..." ) ;
141+ server . close ( ) ;
142+ process . exit ( 0 ) ;
143+ } ) ;
144+
145+ process . on ( "SIGINT" , ( ) => {
146+ logError ( "SIGINT received, closing server..." ) ;
147+ server . close ( ) ;
148+ process . exit ( 0 ) ;
149+ } ) ;
150+ } catch ( error ) {
151+ logError ( "Server startup failed" , error ) ;
152+ process . exit ( 1 ) ;
153+ }
93154}
94155
95- runServer ( ) . catch ( console . error ) ;
156+ logError ( "Initializing MCP server process..." ) ;
157+
158+ runServer ( ) . catch ( ( error ) => {
159+ logError ( "Unhandled server error" , error ) ;
160+ process . exit ( 1 ) ;
161+ } ) ;
0 commit comments