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
Visit Server

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

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

graphlit-mcp-server

The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.

Official
Featured
TypeScript
Kagi MCP Server

Kagi MCP Server

An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

Official
Featured
Python
E2B

E2B

Using MCP to run code via e2b.

Official
Featured
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
Exa Search

Exa Search

A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured