mcp-context-engineering
mcp-context-engineering
MCPサーバー向けコンテキストエンジニアリングを実演する、小さくて実行可能なプロジェクトです。Model Context Protocolサーバーがモデルのコンテキストウィンドウ内で占めるフットプリントを小さく保つことで、エージェントのコストを抑え、精度を高めます。
問題
MCPクライアント(Claude Desktop、Cursor、SDKアプリなど)がMCPサーバーに接続すると、アドバタイズされているすべてのツール定義(名前、説明、完全な入力スキーマ)がモデルのコンテキストに取り込まれます。ツールが30〜60以上あるサーバーでは、エージェントが何かをする前に、定義だけで優に1万トークン以上を消費し得ます。これには2つの問題があります。
トークンの無駄遣い。 エージェントが決して呼び出さないツール定義に対してコストを支払うことになります。
精度の低下。 モデルは無関係なツールに気を取られ、誤ったツールを選んだり、パラメータを捏造したりする可能性が高くなります。
2つのテクニック
このプロジェクトは、論理グループに整理された33個のモック「ウェブデータ」ツール(Amazon、LinkedIn、TikTok、GitHub、Zillow、ブラウザ自動化、バッチスクレイピングなど)のカタログに対して、修正策の両方を実装しています。
アドバタイズするツールをスコープで絞る。 エージェントが必要とする機能だけを読み込みます。グループ全体(
GROUPS=social)で指定するか、個別のツールを手動選択(TOOLS=web_data_amazon_product,...)します。これらの定義だけがコンテキストに到達します。ツールが返す出力を最適化する。 スクレイピングしたページがコンテキストに入る前に、トークンを浪費するMarkdown(太字/斜体、画像構文、見出しマーカー、コードフェンス、リンクURL)を除去し、モデルが実際に読むすべての単語を保持します。
測定された効果(同梱のオフラインレポートより)
フルカタログ = 33ツール ≈ スコープ制限なしで読み込むと、定義は約4,556トークン。
構成 | ツール数 | 定義トークン | 全ツール比での削減 |
デフォルト(基本ツールのみ) | 3 | 506 | 89% |
| 9 | 1,318 | 71% |
| 11 | 1,566 | 66% |
| 14 | 1,973 | 57% |
| 3 | 416 | 91% |
| 6 | 917 | 80% |
| 33 | 4,556 | 0% |
スクレイピングしたページへのstrip-markdown適用: 243 → 149トークン(約39%削減)。
数値は内蔵のヒューリスティックなトークン推定器を使用しています。正確なカウントが必要でtiktokenがインストールされている場合は、レポートに--tiktokenを渡してください。重要なのは比率であり、これは安定しています。
パターンを一言で
読み込むツールをスコープで絞り、返す出力をトリミングし、難しい部分はMCPサーバーに任せる。
コードマップ
mcp-context-engineering/
├── src/mcp_context_engineering/
│ ├── __init__.py # Public API re-exports + version.
│ ├── tool_groups.py # Source of truth for groups: BASE_TOOLS + 8 logical
│ │ # groups (ecommerce, social, business, research,
│ │ # finance, app_stores, browser, advanced_scraping)
│ │ # and helpers (all_tool_names, total_tool_count).
│ ├── tool_catalog.py # Full catalog of 33 ToolSpecs: name, description,
│ │ # JSON input schema, and an OFFLINE mock handler
│ │ # each. Also MARKDOWN_TOOLS (which outputs to strip)
│ │ # and a SAMPLE_MARKDOWN_PAGE for the demo.
│ ├── context_config.py # The scoping brain. Reads PRO_MODE / GROUPS / TOOLS,
│ │ # resolves the exact tool set (resolve_context),
│ │ # and defines named PRESETS.
│ ├── strip_markdown.py # Dependency-free output optimiser: strips Markdown
│ │ # formatting, keeps words + code, links optional.
│ ├── token_utils.py # Lightweight offline token estimator + tool-def
│ │ # token counting (tiktoken optional).
│ └── server.py # The MCP server (official SDK low-level Server,
│ │ # stdio). Advertises only scoped tools; strips
│ │ # Markdown output. build_server() for tests.
├── scripts/
│ ├── run_server.py # Launch the server over stdio (what a client runs).
│ └── token_report.py # Offline demo: prints the savings tables above.
├── examples/
│ ├── claude_desktop_social_agent.json # config: one group
│ ├── claude_desktop_price_monitor.json # config: hand-picked tools
│ └── claude_desktop_pro_mode.json # config: everything (baseline)
├── tests/
│ └── test_context_engineering.py # 23 offline tests (unittest)
├── requirements.txt # Just the official `mcp` SDK (tiktoken optional).
├── .env.example # All config vars, documented.
└── .gitignore各ピースの関係
tool_groups.pyは、どのツール名がどのグループに属するかを定義します。tool_catalog.pyは、各名前に完全な定義(説明+スキーマ)とモックハンドラーを提供します。context_config.pyは環境を読み取り、公開する名前の正確なサブセットを決定します。server.pyはcontext_configにそのサブセットを要求し、tools/list経由でそれらの定義だけをアドバタイズし、MARKDOWN_TOOLSツールが呼び出されたときは、その出力を返す前にstrip_markdown.pyに通します。token_utils.pyはオフラインのtoken_report.pyを支え、ネットワークに触れずに両方の効果を定量化します。
データフロー
flowchart TD
subgraph Config["Configuration (env vars)"]
E["PRO_MODE / GROUPS / TOOLS<br/>STRIP_MARKDOWN"]
end
E --> RC["context_config.resolve_context()"]
TG["tool_groups.py<br/>(group -> tool names)"] --> RC
RC -->|"scoped list of tool names"| SRV["server.py (MCP Server)"]
TC["tool_catalog.py<br/>(name -> description, schema, handler)"] --> SRV
subgraph MCP["MCP session (stdio)"]
CLIENT["MCP client / LLM agent"]
SRV
end
SRV -->|"tools/list: ONLY scoped definitions"| CLIENT
CLIENT -->|"tools/call(name, args)"| SRV
SRV -->|"handler() output"| STRIP["strip_markdown.py<br/>(markdown tools only)"]
STRIP -->|"trimmed text"| CLIENT
RC -.offline.-> REPORT["scripts/token_report.py"]
TC -.offline.-> REPORT
TU["token_utils.py"] -.-> REPORT
REPORT -.-> OUT["savings tables"]クイックスタート
# 1. (optional) create a virtualenv
python -m venv .venv && source .venv/bin/activate # Windows: .venv\Scripts\activate
# 2. install the one dependency
pip install -r requirements.txt
# 3. see the token savings - fully offline, no key, no network
python scripts/token_report.py
python scripts/token_report.py --json # machine-readable
# 4. run the tests
python -m unittest discover -s tests -vMCPサーバーの実行
サーバーはstdio上でMCPを話し、環境変数だけで完全に設定されます:
# default: just the small base tool set
python scripts/run_server.py
# a focused social-media agent
GROUPS=social python scripts/run_server.py
# hand-pick exactly the tools a price monitor needs
TOOLS=web_data_amazon_product,web_data_ebay_product,web_data_google_shopping \
python scripts/run_server.py
# the un-scoped baseline (loads everything)
PRO_MODE=true python scripts/run_server.py
# disable output trimming
STRIP_MARKDOWN=false GROUPS=social python scripts/run_server.py有効なグループID: ecommerce、social、business、research、finance、app_stores、browser、advanced_scraping。変数の完全なリストは.env.exampleを参照してください。
MCPクライアントへの接続
examples/内のファイルの1つをクライアントのサーバー設定(Claude Desktopの場合はclaude_desktop_config.json)にコピーし、/ABSOLUTE/PATHをチェックアウトしたパスに置き換えて、クライアントを再起動します。3つの例は、スコープ指定したグループ、手動選択したセット、すべてを読み込むベースラインを示しています。
ツールに関する注意
このプロジェクトのすべてのツールハンドラーは、固定のオフラインサンプルデータを返します。APIキーもネットワークアクセスもどこにもありません。目的はライブサイトをスクレイピングすることではなく、コンテキストエンジニアリングのパターンを実演することです。実際に使えるようにするには、tool_catalog.pyのハンドラーを実際のウェブデータバックエンドへの呼び出しに置き換え、その認証情報を環境変数(プレースホルダーWEB_DATA_API_KEYが.env.exampleに記載されています)から読み取ります。
基盤 / インスピレーション
Model Context Protocol Python SDK - このサーバーが使用する公式SDK: https://github.com/modelcontextprotocol/python-sdk
プロトコルドキュメントと仕様: https://modelcontextprotocol.io
Bright Data MCP server - ここでモデル化したツールグループのスコープ制限とstrip-markdown出力最適化を広めたオープンソースのMCPサーバー: https://github.com/brightdata/brightdata-mcp
ライセンス
MIT(LICENSEがあればそれを参照。なければサンプルコードはMITライセンスとして扱ってください)。
This server cannot be installed
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Related MCP Connectors
Free public MCP for AI agents — 193 tools, 44 workflows. No API key.
Deterministic AI agent microtools, no accounts/API keys. fetch_extract: 98% token cut. 38 tools.
See, price, and control every tool call your AI agents make: policy checks, cost, and audit tools.
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/crzyc0d3r/mcp-context-engineering'
If you have feedback or need assistance with the MCP directory API, please join our Discord server