Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 3, 2025

This PR adds comprehensive support for MCP (Model Context Protocol) routes to @nestia/core, enabling developers to easily expose TypeScript functions as tools that can be called by LLMs through the standardized MCP protocol.

Features Added

Core Implementation

  • @McpRoute decorator - Marks methods as MCP tools with configurable name, description, and JSON schema
  • McpController base class - Automatically provides MCP protocol endpoints (POST /tools and POST /call)
  • Type-safe tool definitions - Full TypeScript support for parameters and return types
  • JSON Schema support - Manual schema configuration for tool parameter validation

Protocol Compliance

The implementation follows the MCP specification with:

  • list_tools endpoint returning available tools with their schemas
  • call_tool endpoint for executing specific tools with parameters
  • Proper MCP response format with content arrays
  • Standard error codes (-32601 for tool not found, -32602 for invalid parameters)

Usage Example

import core from "@nestia/core";

export class WeatherController extends core.McpController("weather") {
  @core.McpRoute({
    name: "get_weather",
    description: "Get current weather for a location",
    inputSchema: {
      type: "object",
      properties: {
        location: { type: "string", description: "Location to get weather for" },
        unit: { type: "string", enum: ["celsius", "fahrenheit"] }
      },
      required: ["location"]
    }
  })
  public async getWeather(params: { location: string; unit?: string }) {
    return {
      location: params.location,
      temperature: 22,
      unit: params.unit || "celsius",
      conditions: "sunny"
    };
  }
}

This automatically exposes:

  • POST /weather/tools - Lists available tools
  • POST /weather/call - Calls specific tools

Error Handling

  • Comprehensive error handling with proper MCP error responses
  • Tool-level exception catching with structured error messages
  • Parameter validation and type checking

Testing

Added comprehensive test suite with 9 test cases covering:

  • Tool discovery from multiple controllers
  • Parameter validation and tool execution
  • Error handling for invalid tools and parameters
  • Real-world utility functions (UUID generation, Base64 encoding/decoding, JSON validation)

All tests pass, demonstrating full protocol compliance and robust functionality.

Integration with LLMs

The MCP routes can be used with any LLM that supports the Model Context Protocol:

  • OpenAI GPT models (via MCP adapters)
  • Anthropic Claude (native MCP support)
  • Local LLMs with MCP implementations

Fixes #1350.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] Support MCP route functions from @nestia/core Add MCP (Model Context Protocol) route support to @nestia/core Aug 3, 2025
@Copilot Copilot AI requested a review from samchon August 3, 2025 00:30
Copilot finished work on behalf of samchon August 3, 2025 00:30
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.

Support MCP route functions from @nestia/core

2 participants