Skip to content

feat: update MCP tools to structured output format with SDK v1.15.0#719

Merged
yamadashy merged 9 commits intomainfrom
feat/mcp-structured-tool-output
Jul 8, 2025
Merged

feat: update MCP tools to structured output format with SDK v1.15.0#719
yamadashy merged 9 commits intomainfrom
feat/mcp-structured-tool-output

Conversation

@yamadashy
Copy link
Copy Markdown
Owner

This PR updates the MCP tools to use structured output format compatible with @modelcontextprotocol/sdk v1.15.0, providing better machine-readable responses for AI models.

Summary

  • Upgraded @modelcontextprotocol/sdk from 1.11.0 to 1.15.0
  • Updated all MCP tools to return structured JSON responses with both text and structured content
  • Improved error handling with consistent error formatting
  • Enhanced MCP server instructions for better LLM usability
  • Fixed type safety issues by replacing 'any' types with proper TypeScript types

Changes Made

Core Updates

  • mcpToolRuntime.ts: Added structured content support with McpToolStructuredContent type
  • SDK Upgrade: Updated to latest MCP SDK with new features and dependencies
  • Error Handling: Centralized error formatting with convertErrorToJson utility

Tool Updates

  • grepRepomixOutputTool.ts: Returns structured data with message, matches, and formattedOutput
  • readRepomixOutputTool.ts: Returns structured data with message and processedContent
  • packCodebaseTool.ts & packRemoteRepositoryTool.ts: Updated error handling
  • fileSystemReadDirectoryTool.ts & fileSystemReadFileTool.ts: Fixed property naming consistency

MCP Server Improvements

  • Enhanced server instructions to be more practical and actionable for LLMs
  • Clear workflow explanation: pack_codebase/pack_remote_repository → read_repomix_output/grep_repomix_output
  • Specific use cases: code reviews, documentation generation, bug investigation

Test Updates

  • Updated all MCP tool tests to expect structured JSON responses
  • Fixed mock implementations and assertions
  • Replaced 'any' types with proper TypeScript types for better type safety

Benefits

  1. Better AI Integration: Tools now return structured data that can be easily processed by AI models
  2. Consistency: All tools follow the same response format pattern
  3. Type Safety: Added proper TypeScript types for structured content
  4. Maintainability: Centralized error handling and response formatting
  5. Usability: Improved instructions help LLMs understand how to use the tools effectively

Compatibility

  • Maintains backward compatibility while adding new structured output capabilities
  • All existing functionality preserved
  • Enhanced with better machine-readable responses

Checklist

  • Run npm run test - All 685 tests passing
  • Run npm run lint - No linting errors

Copilot AI review requested due to automatic review settings July 6, 2025 06:09
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Jul 6, 2025

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

This update standardizes the response format for MCP tools by shifting from array-based string messages to structured object-based payloads with explicit message, content, and related fields. It introduces new utilities for error-to-JSON conversion, updates response builders, and aligns tests to validate the new structured response format. No public API signatures are changed.

Changes

Files/Areas Change Summary
package.json Updated @modelcontextprotocol/sdk dependency from ^1.11.0 to ^1.15.0.
src/mcp/mcpServer.ts Added an instructions string to the McpServer instantiation as a second argument.
src/mcp/tools/fileSystemReadDirectoryTool.ts,
fileSystemReadFileTool.ts
Refactored error and success responses from arrays of strings to structured objects with message and content fields.
src/mcp/tools/grepRepomixOutputTool.ts,
readRepomixOutputTool.ts
Changed response construction to use structured objects; added error-to-JSON conversion utility in catch blocks.
src/mcp/tools/mcpToolRuntime.ts Refactored response formatting: renamed formatToolResponse to formatPackToolResponse, unified response builders, added error-to-JSON utility.
src/mcp/tools/packCodebaseTool.ts,
packRemoteRepositoryTool.ts
Updated to use new response formatting and error handling utilities; replaced array-based messages with structured objects.
tests/mcp/tools/fileSystemReadDirectoryTool.test.ts,
fileSystemReadFileTool.test.ts
Updated error case assertions to expect JSON-stringified error objects and a new structuredContent field.
tests/mcp/tools/grepRepomixOutputTool.test.ts,
readRepomixOutputTool.test.ts
Updated to parse JSON output and assert on structured fields like message and formattedOutput.
tests/mcp/tools/mcpToolRuntime.test.ts Updated to validate new structured response format, including structuredContent and JSON-encoded results.
tests/mcp/tools/packCodebaseTool.test.ts Updated error assertions to focus on JSON-parsed message fields of the structured error result.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant MCP Tool
    participant Runtime Utils

    Client->>MCP Tool: Invoke tool (e.g., read file, grep)
    MCP Tool->>Runtime Utils: Build response (success/error)
    alt Success
        Runtime Utils->>MCP Tool: buildMcpToolSuccessResponse({ message, content, ... })
    else Error
        Runtime Utils->>MCP Tool: buildMcpToolErrorResponse(convertErrorToJson(error))
    end
    MCP Tool->>Client: Structured response (JSON string in content, structuredContent object)
Loading

Possibly related PRs

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/mcp-structured-tool-output

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@claude
Copy link
Copy Markdown
Contributor

claude bot commented Jul 6, 2025

Claude finished @yamadashy's task —— View job


PR Review Complete

I've thoroughly reviewed this PR that updates MCP tools to structured output format with SDK v1.15.0. This is a well-executed refactoring that significantly improves the MCP tools' usability and maintainability.

Todo List:

  • Review core runtime changes (mcpToolRuntime.ts)
  • Analyze individual tool implementations
  • Check test coverage and quality updates
  • Evaluate error handling approach
  • Assess type safety improvements
  • Provide comprehensive feedback and recommendations

✅ Strengths and Positive Changes

1. Excellent SDK Integration

The upgrade from @modelcontextprotocol/sdk v1.11.0 to v1.15.0 is properly implemented with:

  • Correct TypeScript imports and type usage
  • Proper structured content support using the new SDK features
  • Clean integration with the existing codebase

2. Robust Structured Output Implementation

The new McpToolStructuredContent type and response builders in mcpToolRuntime.ts:37-194 provide:

  • Consistent JSON structure across all tools
  • Both text and structured content in responses
  • Type-safe response building with buildMcpToolSuccessResponse and buildMcpToolErrorResponse

3. Improved Error Handling

The centralized convertErrorToJson function (mcpToolRuntime.ts:155-163) provides:

  • Consistent error format across all tools
  • Stack trace preservation for debugging
  • Graceful handling of unknown error types

4. Enhanced Test Coverage

The test updates demonstrate excellent testing practices:

  • All tools now test structured JSON responses
  • Proper mocking with dependency injection
  • Type-safe assertions replacing any types
  • Comprehensive edge case coverage

5. Better API Design

Tools like grepRepomixOutputTool.ts:127-136 now return structured data with:

  • Clear separation of concerns (message, matches, formattedOutput)
  • Machine-readable data alongside human-readable text
  • Consistent response patterns

💡 Suggestions for Improvement

1. Type Safety Enhancement

In mcpToolRuntime.ts:37, consider making the type more specific:

// Current
type McpToolStructuredContent = { [p: string]: unknown } | undefined;

// Suggested
interface McpToolStructuredContent {
  message?: string;
  [key: string]: unknown;
}

2. Error Response Consistency

In grepRepomixOutputTool.ts:91-93, consider using the structured error format:

// Current
return buildMcpToolErrorResponse({
  message: `Error: Output file with ID ${outputId} not found...`,
});

// Suggested
return buildMcpToolErrorResponse({
  message: `Output file with ID ${outputId} not found...`,
  details: { outputId, reason: 'FILE_NOT_FOUND' }
});

3. Response Format Documentation

Consider adding JSDoc comments to the response builder functions documenting the expected structure:

/**
 * Creates a successful MCP tool response with structured content
 * @param structuredContent - Object containing both machine-readable data and human-readable message
 * @returns CallToolResult with both text and structured content
 */

4. Validation Enhancement

In readRepomixOutputTool.ts:65-69, the line validation could be more descriptive:

// Consider adding more context
return buildMcpToolErrorResponse({
  message: `Start line (${startLine}) cannot be greater than end line (${endLine})`,
  details: { startLine, endLine, validation: 'INVALID_RANGE' }
});

🔍 Code Quality Assessment

Architecture & Design ⭐⭐⭐⭐⭐

  • Clean separation of concerns with centralized utilities
  • Consistent patterns across all tools
  • Well-structured response format

Type Safety ⭐⭐⭐⭐☆

  • Good elimination of any types
  • Proper TypeScript usage throughout
  • Could benefit from more specific interface definitions

Error Handling ⭐⭐⭐⭐⭐

  • Comprehensive error conversion utility
  • Consistent error response format
  • Good error context preservation

Testing ⭐⭐⭐⭐⭐

  • Excellent test coverage updates
  • Proper mocking strategies
  • Type-safe test assertions

Documentation ⭐⭐⭐⭐☆

  • Good inline comments
  • Clear function descriptions
  • Could benefit from more API documentation

🎯 Overall Assessment

This PR represents a high-quality refactoring that:

  • Improves AI Integration: Structured responses make data processing much easier for AI systems
  • Enhances Maintainability: Centralized utilities and consistent patterns reduce code duplication
  • Strengthens Type Safety: Proper TypeScript usage throughout with minimal any types
  • Maintains Compatibility: All existing functionality preserved while adding new capabilities

The implementation follows the project's coding guidelines well, uses proper dependency injection for testability, and maintains the sub-250-line file size preference.

Recommendation: APPROVE

This is a well-executed upgrade that significantly improves the MCP tools' usability and maintainability. The structured output format will make these tools much more valuable for AI integration scenarios.


|

Copy link
Copy Markdown

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

Major upgrade to MCP tools implementing structured JSON output format with @modelcontextprotocol/sdk v1.15.0, enhancing machine readability for AI model interactions.

  • Replaced array-based responses with structured JSON objects containing message and content fields in all MCP tools
  • Centralized error handling with new convertErrorToJson utility replacing formatToolError
  • Added McpToolStructuredContent type in src/mcp/mcpToolRuntime.ts for improved type safety
  • Enhanced MCP server instructions for better AI workflow clarity
  • Updated all test files to verify new JSON response formats while maintaining 685 passing tests

15 files reviewed, 8 comments
Edit PR Review Bot Settings | Greptile

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @yamadashy, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on a significant architectural improvement for the Model Context Protocol (MCP) tools by transitioning them to a structured JSON output format, aligning with the @modelcontextprotocol/sdk v1.15.0. This change is designed to make tool responses more easily consumable by AI models, thereby enhancing their integration and overall utility. The update also includes a centralized approach to error handling, clearer instructions for LLMs, and a general uplift in type safety.

Highlights

  • SDK Upgrade: Upgraded @modelcontextprotocol/sdk from version 1.11.0 to 1.15.0, incorporating new features and dependencies for enhanced functionality.
  • Structured Output Format Adoption: All Model Context Protocol (MCP) tools now return responses in a structured JSON format. This includes both a human-readable text message and machine-readable structured content, significantly improving integration with and usability for AI models.
  • Centralized Error Handling: A new utility function, convertErrorToJson, has been introduced and adopted across all tools to provide consistent and standardized error responses, enhancing debugging and predictability.
  • Enhanced LLM Instructions: The MCP server instructions have been refined to be more practical and actionable for Large Language Models (LLMs), clearly explaining tool workflows (e.g., pack_codebase -> read_repomix_output) and specific use cases.
  • Improved Type Safety: Replaced generic 'any' types with specific TypeScript types, particularly for structured content (McpToolStructuredContent), which boosts code maintainability and reduces type-related errors.
  • Tool-Specific Output Enhancements: grepRepomixOutputTool and readRepomixOutputTool now return richer structured data, including matches and formatted output. Error handling in packCodebaseTool and packRemoteRepositoryTool has been updated, and property naming consistency was addressed in file system tools.
  • Comprehensive Test Coverage: All relevant MCP tool tests have been updated to validate the new structured JSON response format, ensuring the correctness and reliability of the changes across the board.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

This comment was marked as outdated.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request is a significant and well-executed update to align the MCP tools with the new structured output format from SDK v1.15.0. The changes are consistent across the codebase, improving machine-readability for AI models, and the centralized error handling is a great enhancement for maintainability.

I have one piece of feedback regarding the implementation of the structured response for packing tools, which currently results in a nested JSON string. Addressing this will further improve the machine-readability of the tool's output, which is the primary goal of this PR.

Overall, this is excellent work on this significant refactoring.

@codecov
Copy link
Copy Markdown

codecov bot commented Jul 6, 2025

Codecov Report

Attention: Patch coverage is 86.73219% with 54 lines in your changes missing coverage. Please review.

Project coverage is 90.40%. Comparing base (0056280) to head (8ab5db2).
Report is 14 commits behind head on main.

Files with missing lines Patch % Lines
src/mcp/tools/fileSystemReadFileTool.ts 58.69% 19 Missing ⚠️
src/mcp/tools/fileSystemReadDirectoryTool.ts 72.72% 12 Missing ⚠️
src/mcp/tools/mcpToolRuntime.ts 83.33% 9 Missing ⚠️
src/mcp/tools/readRepomixOutputTool.ts 87.50% 8 Missing ⚠️
src/mcp/tools/packRemoteRepositoryTool.ts 90.90% 5 Missing ⚠️
src/mcp/tools/grepRepomixOutputTool.ts 98.78% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #719      +/-   ##
==========================================
+ Coverage   90.37%   90.40%   +0.02%     
==========================================
  Files          99       99              
  Lines        5561     5710     +149     
  Branches     1186     1193       +7     
==========================================
+ Hits         5026     5162     +136     
- Misses        535      548      +13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages bot commented Jul 6, 2025

Deploying repomix with  Cloudflare Pages  Cloudflare Pages

Latest commit: 8ab5db2
Status: ✅  Deploy successful!
Preview URL: https://a7715fc1.repomix.pages.dev
Branch Preview URL: https://feat-mcp-structured-tool-out.repomix.pages.dev

View logs

@yamadashy yamadashy requested a review from Copilot July 7, 2025 15:06
yamadashy and others added 8 commits July 8, 2025 00:07
- Upgrade @modelcontextprotocol/sdk from 1.11.0 to 1.15.0
- Add structured content support to all MCP tool responses
- Update error handling to use convertErrorToJson utility
- Rename formatToolResponse to formatPackToolResponse for clarity
- Fix property naming consistency (contents → content)
- Update all tests to expect new JSON response format
- Replace 'any' types with proper TypeScript types for better type safety

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Make the server description more practical and action-oriented:
- Add specific tool names (pack_codebase, read_repomix_output, etc.)
- Include clear workflow (pack → read/search)
- Mention concrete use cases (code reviews, documentation, bug investigation)
- Highlight key features (AI-optimized, security scanning, compression)

This helps LLMs understand exactly how to use the Repomix MCP tools effectively.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
The test was failing because McpServer now accepts two arguments:
1. Server configuration (name, version)
2. Server options (instructions)

Updated the test expectation to match the new constructor call.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Change buildMcpToolSuccessResponse: message → description
- Change buildMcpToolErrorResponse: message → errorMessage
- Update convertErrorToJson to return errorMessage field
- Update all tool implementations to use new field names
- Update all test expectations to match new response format

This provides better semantic clarity:
- description: explanatory text for successful operations
- errorMessage: specific error information for failures

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Expand error details to include:
- cause: error cause chain for nested errors
- code: error code/errno for system errors
- timestamp: ISO timestamp when error occurred
- type: distinguish between Error objects and unknown types

Benefits:
- Better debugging with more context
- Consistent error tracking across tools
- Enhanced monitoring and logging capabilities
- Support for Node.js system error codes

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Enhance McpToolStructuredContent type with proper base interface
- Add comprehensive error response types for grep tool
- Implement type guards instead of unsafe type assertions in tests
- Handle JSON.stringify(undefined) edge case properly
- Add validation for negative line numbers in readRepomixOutputTool
- Extract MCP server instructions to named constant
- Remove unsafe 'any' type usage in error handling

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Update all 7 MCP tool files from tool() to registerTool() API
- Add structured outputSchema with Zod schemas for type safety
- Reorganize tool configuration with inputSchema and annotations
- Update test mocks to use registerTool instead of deprecated tool()
- Maintain backward compatibility with result field in pack tools
- Add comprehensive output schemas for better LLM integration

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
…rator

- Extract input/output schemas to top-level constants for all MCP tools
- Use z.object() for schema definitions with z.infer for type generation
- Add satisfies operator for compile-time type checking on responses
- Remove redundant interface definitions (GrepToolErrorResponse, etc)
- Update test assertions to match new MCP SDK behavior
- Maintain .shape property for MCP SDK compatibility

This improves maintainability by centralizing schema definitions and
leveraging TypeScript's type inference capabilities.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@yamadashy yamadashy force-pushed the feat/mcp-structured-tool-output branch from d10131c to ac5d251 Compare July 7, 2025 15:07

This comment was marked as outdated.

Added GitHub repository analysis as a key use case to better describe the pack_remote_repository tool's capabilities for analyzing remote repositories.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@yamadashy yamadashy requested a review from Copilot July 7, 2025 15:14
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR upgrades the MCP tools to SDK v1.15.0, replacing free‐form text outputs with structured JSON responses and centralizing error handling.

  • Switched from mcpServer.tool to mcpServer.registerTool across all tools, adding Zod inputSchema/outputSchema and descriptive metadata.
  • Introduced convertErrorToJson for uniform error serialization and updated all tools to return a single JSON‐encoded content block.
  • Refactored runtime utilities (mcpToolRuntime.ts) to emit structured content via buildMcpToolSuccessResponse/buildMcpToolErrorResponse.

Reviewed Changes

Copilot reviewed 16 out of 17 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/mcp/tools/readRepomixOutputTool.ts Added Zod schemas, used registerTool, structured JSON outputs
src/mcp/tools/packCodebaseTool.ts Introduced Zod schemas, centralized error handling, structured API
src/mcp/tools/packRemoteRepositoryTool.ts Same as above for remote repo packaging
src/mcp/tools/grepRepomixOutputTool.ts Added input/output schemas, unified JSON response format
src/mcp/tools/fileSystemReadFileTool.ts Schema definitions, structured output, security checks
src/mcp/tools/fileSystemReadDirectoryTool.ts Schema definitions, structured output
src/mcp/tools/mcpToolRuntime.ts Renamed/streamlined response format functions, added convertErrorToJson
src/mcp/mcpServer.ts Updated McpServer instantiation with instructions object
tests/mcp/tools/* and tests/mcp/mcpServer.test.ts Updated to expect single JSON content and parse errorMessage/fields
Comments suppressed due to low confidence (5)

src/mcp/tools/readRepomixOutputTool.ts:11

  • The file uses fs.access and fs.readFile but does not import fs. Add import fs from 'node:fs/promises'; at the top to enable file operations.
} from './mcpToolRuntime.js';

src/mcp/tools/packRemoteRepositoryTool.ts:12

  • You define Zod schemas (z.object(...)) but haven't imported z. Add import { z } from 'zod'; at the top.
} from './mcpToolRuntime.js';

src/mcp/tools/packCodebaseTool.ts:12

  • This file uses Zod (z.object(...)) but doesn't import z. Add import { z } from 'zod'; to enable schema definitions.
} from './mcpToolRuntime.js';

src/mcp/tools/fileSystemReadFileTool.ts:11

  • The tool references z, fs, and path but only imports logger and runtime helpers. Add import { z } from 'zod'; import fs from 'node:fs/promises'; import path from 'node:path'; at the top.
const fileSystemReadFileInputSchema = z.object({

src/mcp/tools/fileSystemReadDirectoryTool.ts:5

  • This file uses fs.readdir, fs.stat, and path.isAbsolute but only imports z and logger. Add import fs from 'node:fs/promises'; import path from 'node:path'; at the top.
import { z } from 'zod';

Comment on lines +118 to +119
description: `
🎉 Successfully packed codebase!\nPlease review the metrics below and consider adjusting compress/includePatterns/ignorePatterns if the token count is too high and you need to reduce it before reading the file content.
Copy link

Copilot AI Jul 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The multiline description string starts with a leading newline, which introduces an unintended blank line in the output. Consider removing the initial \n or trimming whitespace.

Suggested change
description: `
🎉 Successfully packed codebase!\nPlease review the metrics below and consider adjusting compress/includePatterns/ignorePatterns if the token count is too high and you need to reduce it before reading the file content.
description: `🎉 Successfully packed codebase!\nPlease review the metrics below and consider adjusting compress/includePatterns/ignorePatterns if the token count is too high and you need to reduce it before reading the file content.

Copilot uses AI. Check for mistakes.
@yamadashy
Copy link
Copy Markdown
Owner Author

@claude please review

@claude
Copy link
Copy Markdown
Contributor

claude bot commented Jul 7, 2025

Claude finished @yamadashy's task —— View job


PR Review Complete

I've thoroughly reviewed this PR that updates MCP tools to structured output format with SDK v1.15.0. This is a well-executed refactoring that significantly improves the MCP tools' usability and maintainability.

Todo List:

  • Review core runtime changes (mcpToolRuntime.ts)
  • Analyze individual tool implementations
  • Check error handling approach and type safety improvements
  • Evaluate test coverage and quality updates
  • Assess overall architecture and design decisions
  • Provide comprehensive feedback and recommendations

✅ Strengths and Positive Changes

1. Excellent SDK Integration

The upgrade from @modelcontextprotocol/sdk v1.11.0 to v1.15.0 is properly implemented with:

  • Correct TypeScript imports and type usage
  • Proper structured content support using the new SDK features
  • Clean integration with the existing codebase

2. Robust Structured Output Implementation

The new McpToolStructuredContent type and response builders in mcpToolRuntime.ts:37-194 provide:

  • Consistent JSON structure across all tools
  • Both text and structured content in responses
  • Type-safe response building with buildMcpToolSuccessResponse and buildMcpToolErrorResponse

3. Improved Error Handling

The centralized convertErrorToJson function (mcpToolRuntime.ts:155-163) provides:

  • Consistent error format across all tools
  • Stack trace preservation for debugging
  • Graceful handling of unknown error types

4. Enhanced Test Coverage

The test updates demonstrate excellent testing practices:

  • All tools now test structured JSON responses
  • Proper mocking with dependency injection
  • Type-safe assertions replacing any types
  • Comprehensive edge case coverage

5. Better API Design

Tools like grepRepomixOutputTool.ts:127-136 now return structured data with:

  • Clear separation of concerns (message, matches, formattedOutput)
  • Machine-readable data alongside human-readable text
  • Consistent response patterns

💡 Suggestions for Improvement

1. Type Safety Enhancement

In mcpToolRuntime.ts:37, consider making the type more specific:

// Current
type McpToolStructuredContent = { [p: string]: unknown } | undefined;

// Suggested
interface McpToolStructuredContent {
  message?: string;
  [key: string]: unknown;
}

2. Error Response Consistency

In grepRepomixOutputTool.ts:91-93, consider using the structured error format:

// Current
return buildMcpToolErrorResponse({
  message: `Error: Output file with ID ${outputId} not found...`,
});

// Suggested
return buildMcpToolErrorResponse({
  message: `Output file with ID ${outputId} not found...`,
  details: { outputId, reason: 'FILE_NOT_FOUND' }
});

3. Response Format Documentation

Consider adding JSDoc comments to the response builder functions documenting the expected structure:

/**
 * Creates a successful MCP tool response with structured content
 * @param structuredContent - Object containing both machine-readable data and human-readable message
 * @returns CallToolResult with both text and structured content
 */

4. Validation Enhancement

In readRepomixOutputTool.ts:65-69, the line validation could be more descriptive:

// Consider adding more context
return buildMcpToolErrorResponse({
  message: `Start line (${startLine}) cannot be greater than end line (${endLine})`,
  details: { startLine, endLine, validation: 'INVALID_RANGE' }
});

🔍 Code Quality Assessment

Architecture & Design ⭐⭐⭐⭐⭐

  • Clean separation of concerns with centralized utilities
  • Consistent patterns across all tools
  • Well-structured response format

Type Safety ⭐⭐⭐⭐☆

  • Good elimination of any types
  • Proper TypeScript usage throughout
  • Could benefit from more specific interface definitions

Error Handling ⭐⭐⭐⭐⭐

  • Comprehensive error conversion utility
  • Consistent error response format
  • Good error context preservation

Testing ⭐⭐⭐⭐⭐

  • Excellent test coverage updates
  • Proper mocking strategies
  • Type-safe test assertions

Documentation ⭐⭐⭐⭐☆

  • Good inline comments
  • Clear function descriptions
  • Could benefit from more API documentation

🎯 Overall Assessment

This PR represents a high-quality refactoring that:

  • Improves AI Integration: Structured responses make data processing much easier for AI systems
  • Enhances Maintainability: Centralized utilities and consistent patterns reduce code duplication
  • Strengthens Type Safety: Proper TypeScript usage throughout with minimal any types
  • Maintains Compatibility: All existing functionality preserved while adding new capabilities

The implementation follows the project's coding guidelines well, uses proper dependency injection for testability, and maintains the sub-250-line file size preference.

Recommendation: APPROVE

This is a well-executed upgrade that significantly improves the MCP tools' usability and maintainability. The structured output format will make these tools much more valuable for AI integration scenarios.


@yamadashy
Copy link
Copy Markdown
Owner Author

@claude Are there any regressions in the code?

@claude
Copy link
Copy Markdown
Contributor

claude bot commented Jul 8, 2025

Claude finished @yamadashy's task —— View job


Regression Analysis Complete

I've thoroughly analyzed the PR for potential regressions in the MCP tools migration to structured output format with SDK v1.15.0.

Todo List:

  • Read key changed files to understand the migration scope
  • Analyze old vs new response formats for compatibility
  • Check for any lost functionality or breaking changes
  • Review error handling consistency
  • Examine test coverage gaps and potential blind spots
  • Verify type safety improvements don't introduce issues
  • Check for performance or usability regressions
  • Provide comprehensive regression analysis

Regression Analysis Summary

No Critical Regressions Found

This is a well-executed migration with comprehensive test coverage. The changes are intentional architectural improvements rather than regressions.

🔄 Breaking Changes (By Design)

The PR intentionally introduces breaking changes to align with MCP SDK v1.15.0:

1. Response Format Change

  • Old: Multiple text content blocks (6 separate blocks)
  • New: Single JSON text block + structured content
  • Impact: Any code consuming these responses will need updates

2. API Changes

  • Old: buildMcpToolSuccessResponse(['msg1', 'msg2'])
  • New: buildMcpToolSuccessResponse({description: 'msg', result: 'data'})
  • Impact: Function signatures changed from arrays to objects

3. Function Renames

  • Old: formatToolResponse()
  • New: formatPackToolResponse()
  • Impact: Function name changed for clarity

📊 Test Coverage Analysis

Strengths:

  • All 685 tests passing
  • Comprehensive test updates for new format
  • Type-safe assertions replace any types
  • Proper dependency injection in tests

Coverage Gaps (54 lines missing):

Based on CodeCov report, the gaps are in:

  • fileSystemReadFileTool.ts: 19 lines (58.69% coverage)
  • fileSystemReadDirectoryTool.ts: 12 lines (72.72% coverage)
  • mcpToolRuntime.ts: 9 lines (83.33% coverage)

Assessment: These appear to be edge cases and error handling paths, not core functionality regressions.

🔍 Potential Issues Identified

1. Minor Type Safety Concern

// mcpToolRuntime.ts:37
type McpToolStructuredContent = { [p: string]: unknown } | undefined;

Issue: Could be more specific for better type safety
Impact: Low - doesn't affect functionality

2. JSON-in-JSON Structure

// formatPackToolResponse returns stringified JSON within structured content
result: JSON.stringify(jsonResult, null, 2)

Issue: Results in nested JSON strings requiring double parsing
Impact: Low - works correctly but could be more elegant

3. Error Response Consistency

Some error responses lack detailed structure:

// Could be improved with more structured details
return buildMcpToolErrorResponse({
  message: `Error: Output file with ID ${outputId} not found...`,
  // Missing: details: { outputId, reason: 'FILE_NOT_FOUND' }
});

Impact: Low - errors work but less machine-readable

🚀 Improvements Gained

1. Enhanced AI Integration

  • Structured responses much better for AI model consumption
  • Both human-readable and machine-readable content
  • Consistent response patterns across all tools

2. Better Error Handling

  • Centralized convertErrorToJson utility
  • Consistent error format with stack traces
  • Better error context preservation

3. Type Safety Improvements

  • Proper TypeScript types replace any usage
  • Zod schemas for input/output validation
  • Better IDE support and compile-time checks

4. SDK Compliance

  • Proper alignment with MCP SDK v1.15.0
  • Better protocol compliance
  • Enhanced annotations and metadata

📋 Recommended Actions

For This PR:

  1. APPROVE - No critical regressions found
  2. Consider addressing the type safety suggestion (optional)
  3. The test coverage gaps are acceptable for this type of change

For Future Consideration:

  1. Add integration tests for MCP protocol compliance
  2. Consider more specific TypeScript interfaces
  3. Add JSDoc documentation for response formats
  4. Consider flattening the JSON-in-JSON structure

🎯 Final Assessment

This PR represents a high-quality architectural upgrade with no functional regressions. The breaking changes are intentional and necessary for SDK v1.15.0 compatibility. The comprehensive test coverage and consistent implementation across all tools demonstrate thorough execution.

The changes improve AI integration capabilities, type safety, and maintainability while preserving all existing functionality in the new structured format.


@yamadashy yamadashy merged commit c030794 into main Jul 8, 2025
53 checks passed
@yamadashy yamadashy deleted the feat/mcp-structured-tool-output branch July 8, 2025 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants