Elasticsearch:交易搜索 – MCP
2026年2月24日 | by mebius

在今天的文章里,我们来展示一下如何使用 MCP 来进行交易搜索。
安装
Elasticsearch 及 Kibana
如果你还没有安装好自己的 Elasticsearch 及 Kibana,那么请参考一下的文章来进行安装:
在安装的时候,请选择 Elastic Stack 8.x/9.x进行安装。在安装的时候,我们可以看到如下的安装信息:
在本文中,我们使用 Elastic Stack 9.1.2 来进行展示。
启动白金试用
我们需要使用到内置的 ELSER 模型来向量化我们的数据。我们需要启动白金试用:
这样就启动了白金试用。
下载代码
我们按照如下的方式来下载代码:
git clone https://github.com/liu-xiao-guo/transaction_search_mcp
我们可以看到如下的文件:
$ pwd
/Users/liuxg/python/transaction_search_mcp
$ ls -al
total 32
drwxr-xr-x 11 liuxg staff 352 Feb 10 14:12 .
drwxr-xr-x@ 38 liuxg staff 1216 Feb 10 14:12 ..
-rw-r--r-- 1 liuxg staff 838 Feb 10 14:12 .env.example
drwxr-xr-x 12 liuxg staff 384 Feb 10 14:12 .git
-rw-r--r-- 1 liuxg staff 4050 Feb 10 14:12 .gitignore
-rw-r--r-- 1 liuxg staff 4501 Feb 10 14:12 README.md
drwxr-xr-x 4 liuxg staff 128 Feb 10 14:12 docs
drwxr-xr-x 4 liuxg staff 128 Feb 10 14:12 requirements
drwxr-xr-x 6 liuxg staff 192 Feb 10 14:12 scripts
drwxr-xr-x 5 liuxg staff 160 Feb 10 14:12 src
drwxr-xr-x 3 liuxg staff 96 Feb 10 14:12 tests
我们把 .env.example 拷贝到 .env 文件中去:
# Copy environment template
cp .env.example .env
# Edit .env with your configuration
# - Elasticsearch URL and credentials
# - OpenAI API key (for LLM client)
.env
# Elasticsearch Configuration
ELASTICSEARCH_HOST=https://localhost:9200
ELASTICSEARCH_USERNAME=elastic
ELASTICSEARCH_PASSWORD=LYXAfNkSab8QwsbrriYN
ELASTICSEARCH_API_KEY=X3NZeFJwd0Itb1hyWHlSejg4c3c6bHdwWGhaVTRBRTA1WnF1TUlUd1c2dw==
ELASTICSEARCH_INDEX=banking_transactions
OPEN_AI_KEY=
transaction_search_mcp/
├── src/ # Source code
│ ├── server/ # MCP server implementation
│ │ └── server.py # Main MCP server with transaction search tools
│ └── clients/ # Client applications
│ ├── chat_client.py # Basic Streamlit chat interface
│ └── chat_client_llm.py # Enhanced LLM-powered chat client
├── scripts/ # Setup and utility scripts
│ ├── setup_elasticsearch.py # Basic Elasticsearch setup with test data
│ ├── setup_elasticsearch_llm.py # Enhanced setup with realistic test data
│ ├── run_chat.py # Launch basic chat client
│ └── run_llm_chat.py # Launch LLM-powered chat client
├── tests/ # Test files
│ └── test_server.py # Comprehensive server tests
├── docs/ # Documentation
│ ├── README.md # Basic implementation docs
│ └── README_LLM.md # LLM-enhanced version docs
├── requirements/ # Dependencies
│ ├── requirements.txt # Server dependencies
│ └── client_requirements.txt # Client dependencies
├── .env # Environment variables (not in git)
├── .env.example # Environment template
└── .gitignore tgcode # Git ignore rules
创建虚拟环境
我们使用如下的命令:
$ python -m venv .venv
$ source .venv/bin/activate
(.venv) $ # Server dependencies
pip install -r requirements/requiremtgcodeents.txt
# Client dependencies (for chat interfaces)
pip install -r requirements/client_requirements.txt
设置 Elasticsearch
我们运行如下的命令:
python scripts/setup_elasticsearch.py
我们可以到 Kibana 中进行查看:
我们可以看到有 500 个交易记录。其中的一个交易记录如下:
GET banking_transactions/_doc/txn_000001
{
"_index": "banking_transactions",
"_id": "txn_000001",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"transaction_id": "txn_000001",
"account_id": "acc_001",
"account_type": "savings",
"transaction_date": "2026-01-16",
"posted_date": "2026-01-17",
"amount": -47.64,
"currency": "USD",
"description": "Home Depot - car_wash",
"memo": "Purchase at Home Depot",
"reference": "REF271525",
"merchant": "Home Depot",
"category": "gas",
"subcategory": "car_wash",
"transaction_type": "fee",
"location": {
"city": "Austin",
"state": "tgcodeTX",
"country": "US",
"postal_code": "73301",
"coordinates": {
"lat": 33.40455,
"lon": -96.542508
}
},
"tags": [
"transportation"
],
"balance_after": 3929.25,
"is_pending": false,
"is_recurring": false,
"created_at": "2026-02-10T14:34:40.767Z",
"updated_at": "2026-02-10T14:34:40.767Z"
}
}
如果我们希望使用 LLM 来帮我们生成相应的模拟数据,我们可以运行:
python scripts/setup_elasticsearch_llm.py
这些数据将更符合现实的实际交易。如果你不知道如何设置 LM Studio 来部署大模型,请阅读之前的文章 ”使用 Elastic 和 LM Studio 的 Herding Llama 3.1“。
在我的代码中,我使用 OpenAI 来生成需要的 500 个模拟文档:
(.venv) $ python scripts/setup_elasticsearch_llm_openai.py
(.venv) $ pwd
/Users/liuxg/python/transaction_search_mcp
(.venv) $ python scripts/setup_elasticsearch_llm_openai.py
/Users/liuxg/python/transaction_search_mcp/.venv/lib/python3.11/site-packages/elasticsearch/_sync/client/__init__.py:402: SecurityWarning: Connecting to 'https://localhost:9200' using TLS with verify_certs=False is insecure
_transport = transport_class(
Connecting to Elasticsearch at https://localhost:9200...
/Users/liuxg/python/transaction_search_mcp/.venv/lib/python3.11/site-packages/urllib3/connectionpool.py:1097: InsecureRequestWarning: Unverified HTTPS request is being made to host 'localhost'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
warnings.warn(
✅ Connected to Elasticsearch
/Users/liuxg/python/transaction_search_mcp/.venv/lib/python3.11/site-packages/urllib3/connectionpool.py:1097: InsecureRequestWarning: Unverified HTTPS request is being made to host 'localhost'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
warnings.warn(
Creating index: banking_transactions
/Users/liuxg/python/transaction_search_mcp/.venv/lib/python3.11/site-packages/urllib3/connectionpool.py:1097: InsecureRequestWarning: Unverified HTTPS request is being made to host 'localhost'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
warnings.warn(
✅ Index created successfully
Generating AI-enhanced test transaction data...
✅ Connected to OpenAI - using AI for realistic transaction generation with gpt-4-turbo-preview
Generating 500 realistic transactions...
Generated 0/500 transactions...
Generated 50/500 transactions...
Generated 100/500 transactions...
Generated 150/500 transactions...
Generated 200/500 transactions...
Generated 250/500 transactions...
Generated 300/500 transactions...
Generated 350/500 transactions...
Generated 400/500 transactions...
Generated 450/500 transactions...
✅ Generated 500 transactions
Indexing 500 transactions...
/Users/liuxg/python/transaction_search_mcp/scripts/setup_elasticsearch_llm_openai.py:513: DeprecationWarning: Passing transport options in the API method is deprecated. Use 'Elasticsearch.options()' instead.
success, failed = bulk(es, doc_generator(), chunk_size=50, request_timeout=60)
/Users/liuxg/python/transaction_search_mcp/.venv/lib/python3.11/site-packages/urllib3/connectionpool.py:1097: InsecureRequestWarning: Unverified HTTPS request is being made to host 'localhost'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
warnings.warn(
/Users/liuxg/python/transaction_search_mcp/.venv/lib/python3.11/site-packages/urllib3/connectionpool.py:1097: InsecureRequestWarning: Unverified HTTPS request is being made to host 'localhost'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
warnings.warn(
/Users/liuxg/python/transaction_search_mcp/.venv/lib/python3.11/site-packages/urllib3/connectionpool.py:1097: InsecureRequestWarning: Unverified HTTPS request is being made to host 'localhost'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
warnings.warn(
/Users/liuxg/python/transaction_search_mcp/.venv/lib/python3.11/site-packages/urllib3/connectionpool.py:1097: InsecureRequestWarning: Unverified HTTPS request is being made to host 'localhost'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
warnings.warn(
/Users/liuxg/python/transaction_search_mcp/.venv/lib/python3.11/site-packages/urllib3/connectionpool.py:1097: InsecureRequestWarning: Unverified HTTPS request is being made to host 'localhost'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
warnings.warn(
/Users/liuxg/python/transaction_search_mcp/.venv/lib/python3.11/site-packages/urllib3/connectionpool.py:1097: InsecureRequestWarning: Unverified HTTPS request is being made to host 'localhost'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
warnings.warn(
/Users/liuxg/python/transaction_search_mcp/.venv/lib/python3.11/site-packages/urllib3/connectionpool.py:1097: InsecureRequestWarning: Unverified HTTPS request is being made to host 'localhost'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
warnings.warn(
/Users/liuxg/python/transaction_search_mcp/.venv/lib/python3.11/site-packages/urllib3/connectionpool.py:1097: InsecureRequestWarning: Unverified HTTPS request is being made to host 'localhost'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
warnings.warn(
/Users/liuxg/python/transaction_search_mcp/.venv/lib/python3.11/site-packages/urllib3/connectionpool.py:1097: InsecureRequestWarning: Unverified HTTPS request is being made to host 'localhost'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
warnings.warn(
/Users/liuxg/python/transaction_search_mcp/.venv/lib/python3.11/site-packages/urllib3/connectionpool.py:1097: InsecureRequestWarning: Unverified HTTPS request is being made to host 'localhost'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
warnings.warn(
✅ Successfully indexed 500 transactions
/Users/liuxg/python/transaction_search_mcp/.venv/lib/python3.11/site-packages/urllib3/connectionpool.py:1097: InsecureRequestWarning: Unverified HTTPS request is being made to host 'localhost'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
warnings.warn(
/Users/liuxg/python/transaction_search_mcp/.venv/lib/python3.11/site-packages/urllib3/connectionpool.py:1097: InsecureRequestWarning: Unverified HTTPS request is being made to host 'localhost'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
warnings.warn(
Index 'banking_transactions' now contains 500 documents
Setup completed successfully!
Sample search queries you can try:
- Search for coffee shop transactions
- Find all grocery purchases over $50
- Look for gas station visits in San Francisco
- Find streaming service subscriptions
- Search for business-related expenses
运行 MCP 服务器
我们使用如下的命令来运行服务器:
# Start MCP server
python src/server/server.py
运行客户端
我们运行如下的命令:
python scripts/run_chat.py
我们可以看到如上的界面。我们可以尝试如下的搜索:
我们可以使用由 LLM 驱动的界面:
python scripts/run_llm_chat.py

我再尝试其它的查询:
What did I spend on groceries this year?

查找所有在 San Francisco 超过 $100 的交易

提供过去 3 个月的消费汇总

源码请在地址https://github.com/liu-xiao-guo/transaction_search_mcp下载。
文章来源于互联网:Elasticsearch:交易搜索 – MCP
作者:来自 ElasticElastic DevRel team 来自 Elastic DevRel 团队的问候!在本期通讯中,我们将介绍 Elasticsearch 和 Elastic Stack 的 9.3 版本、最新的博客和视频,以及即将举行的活动,包括…