banditdb-mcp
BanditDB Python SDK
BanditDB — Rust で書かれた超高速・ロックフリーの Contextual Bandit データベース — の公式 Python クライアント兼 Model Context Protocol (MCP) サーバーです。
BanditDB は、強化学習 (LinUCB、Thompson Sampling) の複雑な線形代数を、極めてシンプルな API の背後に隠蔽します。リアルタイムのパーソナライザー、動的な A/B テストを構築し、LLM エージェントに数学的に厳密な永続メモリを提供します。
インストール
pip install banditdb-pythonBanditDB Rust サーバーが実行中である必要があります (デフォルト: http://localhost:8080)。
Related MCP server: Copilot Memory Store
1. 標準 SDK の使用方法
このクライアントは、自動コネクションプーリング、指数バックオフリトライ、厳格なタイムアウトを備えています。
from banditdb import Client, BanditDBError
# Connect to the BanditDB server.
# Pass api_key if BANDITDB_API_KEY is set on the server.
db = Client(
url="http://localhost:8080",
timeout=2.0,
api_key="your-secret-key", # omit if server runs without auth
)
try:
# 1. Create a campaign (run once at startup)
# algorithm defaults to "linucb"; use "thompson_sampling" for Bayesian exploration
db.create_campaign(
campaign_id="checkout_upsell",
arms=["offer_discount", "offer_free_shipping"],
feature_dim=3,
)
# or: db.create_campaign(..., algorithm="thompson_sampling")
# 2. A user arrives — ask the database what to show them
# Context: [is_mobile, cart_value_normalized, is_returning_user]
arm_id, interaction_id = db.predict("checkout_upsell", [1.0, 0.8, 0.0])
print(f"Showing: {arm_id}") # e.g., "offer_free_shipping"
# 3. The user clicked — send the reward
db.reward(interaction_id, reward=1.0)
except BanditDBError as e:
print(f"Database error: {e}")クライアントの全メソッド
ヘルス
メソッド | 説明 |
| サーバーに到達可能で WAL ライターが正常な場合に |
| キャンペーンごとの |
キャンペーン
メソッド | 説明 |
| 新しいキャンペーンを登録します。 |
| すべてのキャンペーン (アクティブおよびアーカイブ済み) のリストを |
| アームごとの完全な状態 ( |
| ビジネスレベルの収束レポートです。 |
| 運用者向け診断情報: アームごとの theta ノルム、 |
| ソフト削除: 予測/報酬を一時停止しますが、学習済みの重みはすべて保持します。 |
| アーカイブ済みキャンペーンをすべての重みを保持したままアクティブ状態に復元します。 |
| キャンペーンを完全に削除します。見つからない場合は |
予測と報酬
メソッド | 説明 |
|
|
| 1 回のラウンドトリップで最大 100 件のキャンペーン/コンテキストペアを予測します。各項目は |
| 結果を記録します。 |
データとエクスポート
メソッド | 説明 |
| WAL をフラッシュし、モデルをスナップショットし、Parquet シャードを書き出し、ニューラル再トレーニング + トーナメント評価を実行し、WAL をローテーションします。サマリー文字列を返します。 |
| キャンペーンごとにグループ化された Parquet エクスポートシャードをリストします。 |
2. AI「ハイブマインド」(Model Context Protocol)
標準的な LLM エージェントはステートレスです。タスクを誤ったモデルにルーティングして失敗した場合、翌日も同じ過ちを繰り返します。BanditDB 組み込みの MCP サーバーは、エージェントの群れ全体に共有の永続メモリを提供します。
MCP サーバーの起動
# Set environment variables before starting
export BANDITDB_URL=http://localhost:8080
export BANDITDB_API_KEY=your-secret-key # omit if server runs without auth
banditdb-mcpClaude Desktop への接続
Claude 設定ファイルに追加します:
Mac:
~/Library/Application Support/Claude/claude_desktop_config.jsonWindows:
%APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"banditdb": {
"command": "banditdb-mcp",
"args": [],
"env": {
"BANDITDB_URL": "http://localhost:8080",
"BANDITDB_API_KEY": "your-secret-key"
}
}
}
}エージェントの群れは、現在 9 つのツールを利用できます:
ツール | 機能 |
| 新しい意思決定キャンペーンを作成します。 |
| すべてのアクティブなキャンペーンをリストします ( |
| アームごとの学習状態 ( |
| ビジネスレベルの収束レポートです。キャンペーンが統計的に収束したかどうか、どのアームが信頼区間付きで勝っているかを示します。 |
| 指定されたコンテキストに対して BanditDB にどのアームを選ぶべきかを尋ねます。アームと保存用の |
| 1 回のラウンドトリップで複数のキャンペーンの意思決定を取得します。 |
| 選択したアクションが成功 (1.0) か失敗 (0.0) かを報告します。共有モデルを更新します。 |
| キャンペーンをソフト削除します。予測/報酬を一時停止しますが、学習済みの重みはすべて保持します。 |
| アーカイブ済みキャンペーンをすべての重みを保持したままアクティブ状態に復元します。 |
ネットワーク内の任意のエージェントによるすべての意思決定は、将来のすべてのエージェントのルーティングを改善します。
3. データサイエンスとオフライン評価
BanditDB はすべての予測と報酬を Write-Ahead Log (WAL) にイベントソーシングします。checkpoint() を呼び出すと、完了した予測→報酬ペアが、キャンペーンごとに 1 つの Snappy 圧縮 Parquet ファイルにコンパイルされ、Polars や Pandas によるオフライン分析に使用できます。
報酬が数時間後に到着した場合でも、すべての予測は Parquet ファイルに確実に含まれます。BanditDB は各チェックポイントで処理中のインタラクションを再出力するため、遅延した報酬は常に将来のサイクルで捕捉されます。
# Checkpoint: snapshot models, write Parquet, rotate the WAL.
# Call this on a schedule or after significant traffic.
summary = db.checkpoint()
print(summary)
# "Checkpoint written and WAL rotated: 2 campaigns, offset 4821 bytes,
# 150 interactions exported, 3 in-flight re-emitted"
# List which Parquet files are available
print(db.export())
# 'Parquet files in /data/exports: ["llm_routing.parquet"]'
# Load directly from the mounted volume into Polars.
# Flat schema: interaction_id | arm_id | reward | predicted_at | rewarded_at | propensity | feature_0 | ...
import polars as pl
df = pl.read_parquet("/data/exports/llm_routing.parquet")
print(df.head())
print(df.columns)オフラインポリシー評価 (OPE)
SDK の banditdb.eval には 3 つの OPE 推定器が同梱されています。これらは次の問いに答えます: 「ライブ実験を実行せずに、別のポリシーの下での平均報酬はどうなっていただろうか?」
評価用の依存関係をインストールします:
pip install "banditdb-python[eval]"推定器 | 関数 | 動作 | 使用場面 |
Replay |
| 各インタラクションを確率 | ベースラインの健全性チェック。カバレッジが低いのは想定どおりで、インタラクションの ~1/K が使用されます。 |
IPS / SNIPS |
| 重要度重み | 主要な推定器。十分なデータがあるが完全なカバレッジが必要な場合に使用します。 |
Doubly Robust |
| 線形報酬モデルを適合させ、残差に IPS 補正を適用します。報酬モデルまたは propensity のいずれかが正しければ、一致推定量です。 | 最良の統計的効率。複数のポリシーを比較する場合や |
3 つの推定器はすべて:
BanditDB の Parquet エクスポートから読み込まれた Polars または pandas DataFrame を受け付ける
ターゲットとして一様ランダムポリシーを評価する(打ち負かすべき不偏ベースライン)
Thompson Sampling キャンペーンでは
ValueErrorを発生させる(propensity 列が null の場合 — TS は propensity を記録しない)estimate、std_error、n_used、n_total、methodを含むOPEResultを返す
import polars as pl
from banditdb.eval import replay, ips, doubly_robust
df = pl.read_parquet("/data/exports/llm_routing.parquet")
# How much reward would a uniform random policy have earned?
print(replay(df))
# OPEResult(method='replay', estimate=0.4821, std_error=0.0312, coverage=22.1% [33/149])
print(ips(df))
# OPEResult(method='ips', estimate=0.5103, std_error=0.0187, coverage=100.0% [149/149])
print(doubly_robust(df))
# OPEResult(method='doubly_robust', estimate=0.5219, std_error=0.0141, coverage=100.0% [149/149])
# Compare against the observed reward of the logging policy:
print("Observed (logging policy):", df["reward"].mean())
# If observed >> estimate, the campaign has learned something real — it outperforms random.実用的な使い方: デプロイ前にオフラインで alpha をスイープする。 実トラフィックでキャンペーンをトレーニングし、Parquet にチェックポイントを保存してから、doubly_robust() でさまざまな alpha 値をリプレイして最適な探索レベルを見つける — ライブ実験は不要。
注: OPE には
propensity列が必要です。この列は LinUCB キャンペーンでのみ書き込まれます。Thompson Sampling キャンペーンではnullの propensity が記録されます。TS のアーム選択は確率的であり、propensity スコアリングには決定論的なロギングポリシーが必要だからです。
アルゴリズムの選択
BanditDB はキャンペーン作成時に選択できる4つのアルゴリズムをサポートしています。
アルゴリズム |
| 探索スタイル | 使用場面 |
LinUCB |
| 決定論的 UCB ボーナス: | 予測可能で調整可能。オフラインで |
Linear Thompson Sampling |
| θ̃ ~ N(θ, α²·A⁻¹) をサンプリングし、θ̃·x でスコアリングする | ベイズ事後分布 — alpha スイープは不要。同時ユーザーは自動的に選択肢を多様化する。 |
NeuralLinUCB |
| 深層 MLP エンベディング + エンベディング空間での LinUCB | 非線形の報酬関数。N 報酬ごとに MLP を再トレーニングする。 |
Progressive |
| 自己調整トーナメント: ベースとチャレンジャーを並列実行し、トラフィックを勝者にシフトする | 設定不要のモデル選択。最適なアルゴリズムを自動的に選択する。 |
from banditdb import Client, NeuralLinUCBConfig, ProgressiveConfig
db = Client("http://localhost:8080")
# LinUCB (default)
db.create_campaign("routing", ["fast", "cheap"], feature_dim=4, alpha=1.5)
# Thompson Sampling — natural Bayesian exploration, alpha=1.0 is ideal
db.create_campaign("routing_ts", ["fast", "cheap"], feature_dim=4,
algorithm="thompson_sampling")
# NeuralLinUCB — learns a deep embedding of the context, then applies LinUCB
cfg = NeuralLinUCBConfig(
context_dim=4, # must match feature_dim
embed_dim=32, # arm matrix dimension (default 32)
hidden_dim=128, # MLP hidden layer width (default 128)
retrain_every=200, # retrain the MLP every N cumulative rewards
)
db.create_campaign("routing_neural", ["fast", "cheap"], feature_dim=4, algorithm=cfg)
# Progressive — runs LinUCB vs NeuralLinUCB, shifts traffic to whoever wins SNIPS checkpoints
cfg = ProgressiveConfig(
base="linucb",
challenger=NeuralLinUCBConfig(context_dim=4, embed_dim=32),
min_obs=100, # minimum buffer entries per arm before any traffic shift
required_wins=3, # consecutive checkpoint wins to earn one traffic step
step_bps=1000, # traffic delta per win run, in basis points (1000 = 10%)
)
db.create_campaign("routing_prog", ["fast", "cheap"], feature_dim=4, algorithm=cfg)4つのアルゴリズムはすべて同じ predict → reward ループを共有しています。
エラーハンドリング
例外 | 発生条件 |
| 基本例外 — すべての SDK エラーを処理するにはこれをキャッチする。 |
| サーバーがオフラインまたは到達不能である。 |
| リクエストが設定されたタイムアウトを超えた。 |
| サーバーがエラーを返した(例: キャンペーンが見つからない、未認可)。 |
ライセンス
Apache-2.0 — Copyright (C) 2026 Simeon Lukov and Dynamic Pricing Ltd. 詳細はメインリポジトリを参照してください。
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections. Dates show when Glama detected each change.
No tool schema history has been recorded yet.
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
Universal memory for AI agents and tools. Save, organize and search context anywhere.
Persistent memory and knowledge management for AI agents with semantic search and 50+ tools.
Shared, governed long-term memory for AI agents across tools and sessions via MCP and REST.
Persistent, inspectable memory for AI agents with lineage, correction, and a hosted MCP endpoint.
Related MCP Servers
- AlicenseAqualityCmaintenanceEnables AI agents to record and rank learnings, facts, and methods through a collaborative voting framework. It provides tools for agents to surface the most useful information across sessions using persistent memory storage.8MIT
- AlicenseNot gradedqualityCmaintenanceEnables storing, searching, and compressing contextual memories for LLM interactions, with tools for memory management and context injection.9MIT
- AlicenseNot gradedqualityDmaintenanceEnables AI agents to store and recall persistent long-term memories across sessions using LanceDB, with semantic search, automatic linking, conflict detection, and maintenance tools.53MIT
- AlicenseNot gradedqualityBmaintenanceEnables AI agents to share persistent, conflict-safe memory by providing tools to recall, learn, reinforce, and retire lessons, using CockroachDB for storage and AWS Bedrock for embeddings.MIT
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/dynamicpricing-ai/banditdb-python'
If you have feedback or need assistance with the MCP directory API, please join our Discord server