Token-Optimized MCP Server

Token-Optimized MCP Server

Minimizes LLM token consumption via YAML serialization, HTML-to-Markdown distillation, and pre-flight token counting, with tools for web content extraction and metrics database querying.

Category
访问服务器

README

Token-Optimized MCP Server

A high-performance Model Context Protocol (MCP) server built with Node.js and TypeScript. This server is purpose-built to minimize LLM token consumption through YAML serialization, structural HTML-to-Markdown distillation, and pre-flight token counting via the BPE tokenizer.


Table of Contents


Overview

Modern AI agents connected via MCP suffer from context window exhaustion when tool responses contain verbose JSON payloads or raw HTML. This server addresses the problem at the architecture layer:

Optimization Strategy Token Reduction
YAML serialization over JSON ~40–48%
HTML → structured Markdown ~60–80%
Pre-flight token gating Prevents overflow

All diagnostic logging is routed to stderr to preserve the JSON-RPC protocol integrity on stdout.


Available Tools

extract_web_content

Fetches a web page, strips HTML noise, and returns clean, semantically structured Markdown optimized for LLM consumption.

Parameter Type Required Description
url string Yes A valid URL to fetch and convert.

Example input:

{
  "url": "https://example.com"
}

Returns: Token-efficient Markdown with preserved heading hierarchy, links, and tables. Token count is logged to stderr.


query_metrics_database

Queries an internal metrics database and returns the results serialized in YAML format to reduce token overhead by approximately 45% compared to equivalent JSON.

Parameter Type Required Description
query string Yes A natural-language or structured query string.

Example input:

{
  "query": "Show CPU and memory usage for the last hour"
}

Returns: YAML-formatted metrics payload. If the response exceeds 8,000 tokens, a warning is emitted to stderr recommending semantic chunking.


Prerequisites

  • Node.js v18.0.0 or higher
  • npm v9+ (bundled with Node.js 18+)

Verify your installation:

node --version   # Must be >= 18.0.0
npm --version

Installation

# Clone or navigate to the project directory
cd optimized-mcp-server

# Install all dependencies
npm install

Dependencies at a Glance

Package Purpose
@modelcontextprotocol/sdk Official MCP server SDK
zod Runtime input schema validation
js-tiktoken BPE token counting (OpenAI compatible)
node-html-markdown High-fidelity HTML → Markdown conversion
@kreuzberg/html-to-markdown Native Rust-binding Markdown converter
yaml JSON → YAML serialization

Building

Compile the TypeScript source to JavaScript:

npm run build

The compiled output is written to the build/ directory.


Configuration

To connect this server to an MCP-compatible AI host, register it in the host's configuration file. Below are copy-pasteable templates for common hosts.

Claude Desktop

macOS: ~/Library/Application Support/Claude/claude_desktop_config.json Windows: %APPDATA%\Claude\claude_desktop_config.json

{
  "mcpServers": {
    "token-optimized-server": {
      "command": "node",
      "args": [
        "/absolute/path/to/optimized-mcp-server/build/index.js"
      ]
    }
  }
}

Important: Replace /absolute/path/to/ with the actual absolute path on your system. On Windows, use double backslashes (\\) or forward slashes (/) in JSON strings.

After saving, fully quit and restart Claude Desktop to re-initialize the JSON-RPC handshake.


VS Code / Antigravity IDE

Add the following to your MCP configuration file (typically mcp_config.json in your editor's settings directory):

{
  "mcpServers": {
    "token-optimized-server": {
      "command": "node",
      "args": [
        "/absolute/path/to/optimized-mcp-server/build/index.js"
      ]
    }
  }
}

Windows example:

"args": [
    "C:\\Users\\YourName\\projects\\optimized-mcp-server\\build\\index.js"
]

Usage

Development Mode

Run the server directly from TypeScript source using tsx (no build step required):

npm run dev

Production Mode

Build first, then start the compiled server:

npm run build
npm start

MCP Inspector

The MCP Inspector provides a browser-based UI for testing tools, simulating LLM requests, and inspecting JSON-RPC messages — no API keys required.

npx @modelcontextprotocol/inspector node build/index.js

This launches a local proxy and opens the Inspector UI in your default browser. Use it to:

  1. Verify the protocol handshake completes successfully.
  2. Execute extract_web_content with a test URL.
  3. Execute query_metrics_database and confirm YAML output.
  4. Monitor stderr logs for token counts and threshold warnings.

Project Structure

optimized-mcp-server/
├── src/
│   └── index.ts          # Core server implementation
├── build/                # Compiled JavaScript output (generated)
├── node_modules/         # Dependencies (generated)
├── package.json          # Project metadata and scripts
├── tsconfig.json         # TypeScript compiler configuration
├── .gitignore            # Git exclusion rules
├── README.md             # This file
└── SECURITY.md           # Security guidelines and best practices

License

ISC

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选