rest-api-mcp-ts

rodrigolima1910/rest-api-mcp-ts

3.2

If you are the rightful owner of rest-api-mcp-ts and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.

This project implements a REST API server using TypeScript, designed to interact with the Model Context Protocol (MCP).

Tools
  1. rest_api_call

    Execute REST API calls with comprehensive response formatting

REST API MCP (Model Context Protocol) - TypeScript Server

This project implements a REST API server using TypeScript, designed to interact with the Model Context Protocol (MCP). It provides a flexible and extensible framework for building backend services that can expose tools and resources compatible with the MCP specification.

Description

The rest-api-mcp-ts is a foundational component for developing MCP-enabled services. It leverages modern TypeScript features and robust libraries to create a high-performance, scalable, and maintainable API. This server can be extended to provide various functionalities, acting as a bridge between external systems and the MCP ecosystem.

Features

  • MCP Compatibility: Built to adhere to the Model Context Protocol specification, allowing seamless integration with MCP clients and other MCP servers.
  • RESTful API Design: Follows REST principles for clear, consistent, and stateless communication.
  • TypeScript: Developed with TypeScript for enhanced code quality, type safety, and maintainability.
  • Modular Structure: Organized into logical modules for easy extension and understanding.
  • Zod for Schema Validation: Utilizes Zod for robust runtime schema validation, ensuring data integrity for API requests and responses.
  • Axios for HTTP Requests: Employs Axios for making efficient and reliable HTTP requests to external services.
  • Express.js (via MCP SDK): Leverages Express.js functionalities through the MCP SDK for handling HTTP routes and middleware.

Technologies Used

  • Node.js: JavaScript runtime environment.

  • TypeScript: Superset of JavaScript that adds static types.

  • @modelcontextprotocol/sdk: Official SDK for interacting with the Model Context Protocol.

  • Axios: Promise-based HTTP client for the browser and Node.js.

  • Zod: TypeScript-first schema declaration and validation library.

  • Express.js: Fast, unopinionated, minimalist web framework for Node.js (integrated via MCP SDK).

Installation

To set up the project locally, follow these steps:

  1. Clone the repository:

    git clone https://github.com/your-username/rest-api-mcp-ts.git
    cd rest-api-mcp-ts
    
  2. Install dependencies:

    npm install
    # or
    yarn install
    
  3. Build the TypeScript project:

    npm run build
    # or
    yarn build
    

Usage

To run the MCP server:

node dist/index.js

This will start the server, making its MCP tools and resources available. You can then interact with it using an MCP client.

Example MCP Tool Usage

This server is designed to be used as an MCP server. An example of how to define and use a tool within this framework is shown in src/index.ts.

// Example from src/index.ts
import { createServer } from '@modelcontextprotocol/sdk';
import { z } from 'zod';
import axios from 'axios';

const server = createServer({
  name: 'rest-api-mcp-ts',
  description: 'A REST API server for Model Context Protocol interactions.',
});

server.addTool({
  name: 'rest_api_call',
  description: 'Execute REST API calls with comprehensive response formatting',
  inputSchema: z.object({
    method: z.enum(['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS']).default('GET').describe('HTTP method (GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS)'),
    url: z.string().url().describe('API endpoint URL'),
    headers: z.record(z.string()).optional().describe('HTTP headers as key-value pairs'),
    body: z.string().optional().describe('Request body (JSON string for POST/PUT/PATCH)'),
    timeout: z.number().int().min(1).max(300).default(30).describe('Request timeout in seconds (default: 30)'),
  }),
  async execute({ method, url, headers, body, timeout }) {
    try {
      const response = await axios({
        method,
        url,
        headers: {
          'Content-Type': 'application/json',
          ...headers,
        },
        data: body,
        timeout: timeout * 1000, // Convert to milliseconds
      });

      return {
        status: response.status,
        statusText: response.statusText,
        headers: response.headers,
        data: response.data,
      };
    } catch (error: any) {
      return {
        error: error.message,
        statusCode: error.response?.status,
        statusText: error.response?.statusText,
        responseHeaders: error.response?.headers,
        responseData: error.response?.data,
      };
    }
  },
});

server.listen(3000, () => {
  console.log('MCP REST API Server listening on port 3000');
});

Project Structure

rest-api-mcp-ts/
ā”œā”€ā”€ dist/                 # Compiled JavaScript output
ā”œā”€ā”€ node_modules/         # Installed Node.js modules
ā”œā”€ā”€ src/                  # Source code
│   └── index.ts          # Main application file
ā”œā”€ā”€ package.json          # Project metadata and dependencies
ā”œā”€ā”€ package-lock.json     # Exact dependency versions
ā”œā”€ā”€ tsconfig.json         # TypeScript compiler configuration
└── README.md             # Project documentation (this file)

Contribution

Contributions are welcome! Please follow these steps to contribute:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/your-feature-name).
  3. Make your changes.
  4. Ensure your code adheres to the project's coding standards.
  5. Write and run tests.
  6. Commit your changes (git commit -m 'feat: Add new feature').
  7. Push to the branch (git push origin feature/your-feature-name).
  8. Open a Pull Request.

License

This project is licensed under the MIT License. See the file for details.

Contact

For any questions or inquiries, please contact:

Rodrigo Lima