Elasticsearch:交易搜索 - MCP

在今天的文章里,我们来展示一下如何使用 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=<YourOpenAIKey>

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                 # Git ignore rules

创建虚拟环境

我们使用如下的命令:

复制代码
$ python -m venv .venv
$ source .venv/bin/activate
(.venv) $ # Server dependencies
pip install -r requirements/requirements.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": "TX",
      "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

这些数据将更符合现实的实际交易。

运行 MCP 服务器

我们使用如下的命令来运行服务器:

复制代码
# Start MCP server
python src/server/server.py

运行客户端

我们运行如下的命令:

复制代码
python scripts/run_chat.py

​

我们可以看到如上的界面。我们可以尝试如下的搜索:

​

​

我们可以使用由 LLM 驱动的界面:

复制代码
python scripts/run_llm_chat.py

源码请在地址 https://github.com/liu-xiao-guo/transaction_search_mcp 下载。

相关推荐
johnsong8 小时前
AI前沿日报 · 2026-10-31 | 安全失守——当AI工具被下毒、当云端门户洞开
人工智能·安全
飞飞传输9 小时前
飞驰云联文件交换建设方法论:从单点传输到持续治理
大数据·运维·安全
得帆低代码PaaS9 小时前
企业级 Agent 架构:MCP、Integration、数据与治理能力
大数据·人工智能·架构·ai agent·企业数字化·mcp
叼烟扛炮9 小时前
Linux 学习笔记(五十六):IO 多路复用初探——select 的“三宗罪”
linux·服务器·数据库·笔记·学习·php
一条大祥脚10 小时前
TVM 的前世今生:从深度学习编译器到 AI 基础设施
人工智能·深度学习·tvm
xiaoye-duck10 小时前
《C++ 项目:手写大模型接入 SDK》Gemini接口设计(上):初始化、全量响应实现和测试
开发语言·网络·c++·语言模型
冬奇Lab11 小时前
一天一个开源项目(第255篇):Mika —— 装在安卓手机里的 AI 代理,在后台虚拟屏上替你操作 App 和网页
人工智能·开源·资讯
.痞老板.12 小时前
mysql初阶--数据库基础
数据库·mysql
孟健13 小时前
实测 agent-desktop:AI 控制桌面不截图、不占鼠标键盘,连点 55 次计算器 0 失误
人工智能·架构·开源
nice_lcj52013 小时前
Java Lambda 表达式入门:怎么写、怎么用、常用方法有哪些
java·开发语言·后端·面试