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 下载。

相关推荐
ZHOUPUYU3 小时前
PHP 8.3网关优化:我用JIT将QPS提升300%的真实踩坑录
开发语言·php
九.九7 小时前
ops-transformer:AI 处理器上的高性能 Transformer 算子库
人工智能·深度学习·transformer
春日见7 小时前
拉取与合并:如何让个人分支既包含你昨天的修改,也包含 develop 最新更新
大数据·人工智能·深度学习·elasticsearch·搜索引擎
小高不会迪斯科7 小时前
CMU 15445学习心得(二) 内存管理及数据移动--数据库系统如何玩转内存
数据库·oracle
恋猫de小郭7 小时前
AI 在提高你工作效率的同时,也一直在增加你的疲惫和焦虑
前端·人工智能·ai编程
寻寻觅觅☆7 小时前
东华OJ-基础题-106-大整数相加(C++)
开发语言·c++·算法
deephub8 小时前
Agent Lightning:微软开源的框架无关 Agent 训练方案,LangChain/AutoGen 都能用
人工智能·microsoft·langchain·大语言模型·agent·强化学习
e***8908 小时前
MySQL 8.0版本JDBC驱动Jar包
数据库·mysql·jar
l1t8 小时前
在wsl的python 3.14.3容器中使用databend包
开发语言·数据库·python·databend
大模型RAG和Agent技术实践8 小时前
从零构建本地AI合同审查系统:架构设计与流式交互实战(完整源代码)
人工智能·交互·智能合同审核