TypeScript Extractor MCP Server

TypeScript Extractor MCP Server

Enables to extract TypeScript methods, functions, and their relevant imports and class properties for code analysis, refactoring, and documentation.

Category
访问服务器

README

TypeScript Extractor MCP Server

An MCP (Model Context Protocol) server for extracting TypeScript imports and method bodies. This tool runs as an HTTP server with authentication and provides tools to extract specific methods/functions along with their relevant imports and class properties from TypeScript files.

Features

  • HTTP-based MCP Server: Uses Streamable HTTP transport for stateful communication
  • Authentication: Bearer token authentication with client registration
  • List Methods Tool: Discover all methods/functions in a TypeScript file
  • Extract Method Tool: Extract methods with their dependencies
  • Read File Tool: Read entire file contents
  • Smart Import Filtering: Returns only project-relative imports that are actually used
  • Class Property Extraction: Extracts properties from the method's class and referenced classes
  • Comprehensive Method Detection: Handles class methods (instance & static), standalone functions, arrow functions, and interface/type method signatures
  • Plain Text Output: Returns readable code with clear separation between imports, properties, and method body

Architecture

This is an HTTP-based MCP server that:

  • Runs on a configurable port (default: 4001)
  • Requires client registration for authentication
  • Uses Bearer tokens for API security
  • Persists tokens to .mcp-tokens.json

Installation

npm install
npm run build

Running the Server

npm start

The server will start on port 4001. You can customize the port:

PORT=3000 npm start

Quick Start

1. Start the Server

npm start

2. Register a Client

curl -X POST http://localhost:4001/register \
  -H "Content-Type: application/json" \
  -d '{"clientId": "my-client"}'

Save the returned token.

3. List Methods in a File

curl -X POST http://localhost:4001/mcp \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "tools/call",
    "params": {
      "name": "list_typescript_methods",
      "arguments": {"filePath": "test-examples/sample.ts"}
    }
  }'

4. Extract a Method

curl -X POST http://localhost:4001/mcp \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 2,
    "method": "tools/call",
    "params": {
      "name": "extract_typescript_method",
      "arguments": {
        "filePath": "test-examples/sample.ts",
        "methodName": "fetchUser"
      }
    }
  }'

API Endpoints

Endpoint Method Auth Description
/register POST No Register client and obtain Bearer token
/mcp POST Bearer Call MCP tools
/mcp GET Bearer SSE streaming endpoint

Available Tools

list_typescript_methods

Lists all method and function names in a TypeScript file.

Parameters:

  • filePath (string, required): Path to the TypeScript file

Example:

curl -X POST http://localhost:4001/mcp \
  -H "Authorization: Bearer TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "tools/call",
    "params": {
      "name": "list_typescript_methods",
      "arguments": {"filePath": "src/app.ts"}
    }
  }'

extract_typescript_method

Extracts a method/function with its relevant imports and class properties.

Parameters:

  • filePath (string, required): Path to the TypeScript file
  • methodName (string, required): Name of the method/function to extract

Example:

curl -X POST http://localhost:4001/mcp \
  -H "Authorization: Bearer TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "tools/call",
    "params": {
      "name": "extract_typescript_method",
      "arguments": {
        "filePath": "src/app.ts",
        "methodName": "handleRequest"
      }
    }
  }'

Output Format:

Method Name: handleRequest
File Path: src/app.ts
Line Number: 45

Imports:
import { helper } from './utils';

Properties/Constants:
private config: Config;
static MAX_RETRIES = 3;

Method Body:
async handleRequest(req: Request): Promise<Response> {
  // method body
}

read_file

Reads and returns the entire contents of a file.

Parameters:

  • filePath (string, required): Path to the file

Example:

curl -X POST http://localhost:4001/mcp \
  -H "Authorization: Bearer TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "tools/call",
    "params": {
      "name": "read_file",
      "arguments": {"filePath": "src/app.ts"}
    }
  }'

Output Format:

[Full file contents]

Supported TypeScript Constructs

  • Class methods (instance methods)
  • Static class methods
  • Standalone function declarations
  • Arrow functions assigned to variables
  • Function expressions assigned to variables
  • Interface method signatures
  • Type alias method signatures

Development

# Watch mode for development
npm run watch

# Build for production
npm run build

# Run tests
npm test

How It Works

  1. Parse: Uses ts-morph to parse TypeScript files into an AST
  2. Locate: Finds the requested method across all supported constructs
  3. Analyze: Identifies all identifiers used in the method body
  4. Filter Imports: Returns only project-relative imports (./, ../, src/) that are used
  5. Extract Properties: For class methods, extracts properties from the class and referenced classes
  6. Format: Returns structured output with imports, properties, and method body

Use Cases

  • Learning & Exploration: Quickly understand how specific methods work and what they depend on
  • Code Review: Extract methods with their dependencies for focused review
  • Documentation: Generate examples showing method implementations with required imports
  • Refactoring: See exact dependencies before moving code to different files
  • Code Navigation: List all methods in a file to understand its structure

Project Structure

context-tree/
├── src/
│   ├── index.ts           # MCP HTTP server
│   └── extractor.ts       # TypeScript extraction logic
├── test-examples/
│   ├── sample.ts          # Example TypeScript file
│   └── test-extractor.ts  # Test script
├── build/                 # Compiled JavaScript
├── package.json
├── tsconfig.json
└── .mcp-tokens.json       # Stored authentication tokens

Dependencies

  • @modelcontextprotocol/sdk: MCP protocol implementation
  • ts-morph: TypeScript AST manipulation
  • express: Web server
  • zod: Schema validation

Documentation

  • QUICKSTART.md - Get up and running in minutes
  • USAGE.md - Detailed usage guide with examples
  • PROJECT_SUMMARY.md - Architecture and technical details

License

MIT

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选