Japanese Character Counter MCP Server
Accurately counts Japanese text characters using grapheme clusters, properly handling surrogate pairs and combining characters through the Intl.Segmenter API.
README
日本語文字数カウント MCP サーバー
Web版ChatGPTの「新しいコネクター」から接続できるMCPサーバーです。日本語テキストの文字数(グラフェム単位)を正確にカウントするツール countJapaneseChars を提供します。
機能
- countJapaneseChars: 日本語テキストの文字数(グラフェム単位)を正確にカウント
Intl.Segmenterを使用してグラフェムクラスターを正確にカウント- サロゲートペアや結合文字を適切に処理
技術スタック
- Next.js 16 (App Router)
- TypeScript
- @modelcontextprotocol/sdk
- Intl.Segmenter (グラフェムカウント用)
セットアップ
依存関係のインストール
npm install
開発サーバーの起動
npm run dev
開発サーバーは http://localhost:3000 で起動します。
MCPサーバーのエンドポイントは http://localhost:3000/api/mcp です。
使用方法
Web版ChatGPTの「新しいコネクター」から接続
- ChatGPTの設定で「新しいコネクター」を開く
- エンドポイントURLを入力:
http://localhost:3000/api/mcp(開発環境) またはhttps://your-domain.com/api/mcp(本番環境) - 接続を確立
APIエンドポイント
- POST
/api/mcp: MCPリクエストを処理 - GET
/api/mcp: SSEストリーム(現在はJSONレスポンスモードを使用) - DELETE
/api/mcp: セッション終了 - OPTIONS
/api/mcp: CORSプリフライト
ツールの使用例
countJapaneseChars ツールを呼び出すと、以下のようなレスポンスが返されます:
{
"content": [
{
"type": "text",
"text": "文字数: 9"
}
],
"structuredContent": {
"count": 9,
"text": "こんにちは、世界!"
}
}
デプロイ
Vercelへのデプロイ(推奨)
1. Vercelアカウントの準備
Vercelにサインアップまたはログインします。
2. プロジェクトのデプロイ
# Vercel CLIをインストール(初回のみ)
npm install -g vercel
# プロジェクトディレクトリでデプロイ
vercel
# 本番環境へデプロイ
vercel --prod
または、GitHubと連携して自動デプロイ:
- GitHubにプロジェクトをプッシュ
- Vercelダッシュボードで「Import Project」をクリック
- GitHubリポジトリを選択
- デプロイ設定を確認(デフォルトで問題なし)
- 「Deploy」をクリック
3. デプロイ後の確認
デプロイ後、提供されたURLの /api/mcp エンドポイントがMCPサーバーとして機能します。
例: https://your-project.vercel.app/api/mcp
4. Web版ChatGPTとの接続
- ChatGPTの設定を開く
- 「新しいコネクター」を選択
- エンドポイントURLを入力:
https://your-project.vercel.app/api/mcp - 接続を確立
その他のデプロイ先
Netlify
# Netlify CLIをインストール
npm install -g netlify-cli
# デプロイ
netlify deploy --prod
Railway
- Railwayにサインアップ
- 「New Project」→「Deploy from GitHub repo」
- ビルドコマンド:
npm run build - 開始コマンド:
npm start
環境変数
現在、環境変数の設定は不要です。本番環境でセッション管理を改善する場合は、Redisなどの外部ストレージを使用することを推奨します。
Redis統合(オプション)
本番環境で複数インスタンスを使用する場合、Redisでセッション管理を行うことを推奨:
npm install ioredis
app/api/mcp/route.ts でRedisを使用するようにコードを修正してください。
実装の詳細
コード構造(モジュール分割)
app/api/mcp/
├── route.ts # HTTPエンドポイントハンドラー
│ # POST, GET, DELETE, OPTIONSメソッド
│ # セッション管理
│
├── lib/
│ ├── helpers.ts # ヘルパー関数
│ │ # - Next.js ⇔ Node.js 変換
│ │ # - CORS設定
│ │
│ └── mcp-server.ts # MCPサーバーセットアップ
│ # - サーバーインスタンス作成
│ # - ツール登録
│
└── tools/
└── countJapaneseChars.ts # 文字数カウントツール
# - グラフェムカウント実装
# - ツール設定とハンドラー
各ファイルには超詳細なコメントが付いており、以下を説明:
- なぜそのコードが必要か
- どのように動作するか
- 注意点や代替案
- 具体的な使用例
主要機能
- グラフェムカウント:
Intl.Segmenterを使用して、人が1文字と捉える単位(グラフェム)を正確にカウント- フォールバック機能付き:
Intl.Segmenterが利用できない環境でも動作
- フォールバック機能付き:
- セッション管理: メモリ内でセッションを管理(本番環境ではRedis等の使用を推奨)
- CORS: Web版ChatGPTからの接続を許可するため、適切なCORSヘッダーを設定
Authorizationヘッダーにも対応(将来の認証機能に備える)
プロダクション品質の特徴
- ランタイム指定:
runtime = "nodejs"でNode.js APIを確実に使用 - 堅牢な初期化:
isInitializeRequestを検出して無条件で新規セッションを作成 - エラーハンドリング: すべてのエンドポイントで適切なエラーレスポンスを返却
- ヘルスチェック: GET エンドポイントでセッション状態を確認可能
- クリーンなセッション削除: DELETE で不要な処理を行わずシンプルに削除
- モジュール分割: 責務ごとにファイルを分割、保守性と可読性を向上
ライセンス
MIT
Recommended Servers
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.
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.
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.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
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.
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.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
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.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.