add_comment
Add a comment to a GitHub issue within a Kanban board workflow, and optionally update the issue state to open or closed.
Instructions
タスクにコメントを追加
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repo | Yes | GitHubリポジトリ名 | |
| issue_number | Yes | タスク(Issue)のID | |
| body | Yes | コメントの内容(Markdown形式対応) | |
| state | No | コメント時に変更するissueの状態(オプション) |
Implementation Reference
- src/handlers/comment-handlers.ts:8-71 (handler)The main handler function for the 'add_comment' tool. It optionally changes issue state (open/closed) via `gh issue close/reopen`, then adds a comment using `gh issue comment` with a temp file, and finally returns updated issue data.
export async function handleAddComment(args: { repo: string; issue_number: string; body: string; state?: 'open' | 'closed'; }): Promise<ToolResponse> { const tempFile = 'comment_body.md'; try { // ステータスの変更が指定されている場合は先に処理 if (args.state) { try { const command = args.state === 'closed' ? 'close' : 'reopen'; await execAsync( `gh issue ${command} ${args.issue_number} --repo ${args.repo}` ); console.log(`Issue status changed to ${args.state}`); } catch (error) { console.error('Failed to change issue status:', error); throw new McpError( ErrorCode.InternalError, `Failed to change issue status: ${(error as Error).message}` ); } } // コメントを追加 const fullPath = await writeToTempFile(args.body, tempFile); try { await execAsync( `gh issue comment ${args.issue_number} --repo ${args.repo} --body-file "${fullPath}"` ); } catch (error) { console.error('Failed to add comment:', error); throw new McpError( ErrorCode.InternalError, `Failed to add comment: ${(error as Error).message}` ); } // 更新後のissue情報を取得して返却 try { const { stdout: issueData } = await execAsync( `gh issue view ${args.issue_number} --repo ${args.repo} --json number,title,state,url` ); return { content: [ { type: 'text', text: issueData, }, ], }; } catch (error) { console.error('Failed to get issue data:', error); throw new McpError( ErrorCode.InternalError, `Failed to get issue data: ${(error as Error).message}` ); } } finally { await removeTempFile(tempFile); } } - src/schemas/comment-schemas.ts:1-23 (schema)Input schema for the 'add_comment' tool, defining parameters: repo (string), issue_number (string), body (string), and optional state (open/closed). Required: repo, issue_number, body.
export const addCommentSchema = { type: 'object', properties: { repo: { type: 'string', description: 'GitHubリポジトリ名', }, issue_number: { type: 'string', description: 'タスク(Issue)のID', }, body: { type: 'string', description: 'コメントの内容(Markdown形式対応)', }, state: { type: 'string', enum: ['open', 'closed'], description: 'コメント時に変更するissueの状態(オプション)', }, }, required: ['repo', 'issue_number', 'body'], }; - src/server.ts:56-60 (registration)Registration of the 'add_comment' tool in the server, including its name, description ('タスクにコメントを追加'), and inputSchema (addCommentSchema).
{ name: 'add_comment', description: 'タスクにコメントを追加', inputSchema: addCommentSchema, }, - src/handlers/tool-handlers.ts:75-84 (handler)Routing logic in the tool handler dispatcher: when tool name is 'add_comment', validates required params (issue_number, body) and calls handleAddComment with the full repo name and args.
case 'add_comment': { if (!args?.issue_number || !args?.body) { throw new McpError(ErrorCode.InvalidParams, 'Issue number and body are required'); } return await handleAddComment({ repo: fullRepo, issue_number: args.issue_number as string, body: args.body as string, }); } - src/utils/exec.ts:13-28 (helper)Helper function writeToTempFile used by handleAddComment to write the comment body to a temporary file before passing it to the gh CLI command.
export async function writeToTempFile(content: string, filePath: string): Promise<string> { const tmpDir = join(process.cwd(), 'tmp'); const fullPath = join(tmpDir, filePath); // tmpディレクトリが存在しない場合は作成 try { await fs.access(tmpDir); } catch { await fs.mkdir(tmpDir, { recursive: true }); } // ファイルに内容を書き込む await fs.writeFile(fullPath, content, 'utf-8'); return fullPath; }