Elasticsearch:交易搜索 - MCP

在今天的文章里,我们来展示一下如何使用 MCP 来进行交易搜索。

安装

Elasticsearch 及 Kibana

如果你还没有安装好自己的 Elasticsearch 及 Kibana,那么请参考一下的文章来进行安装:

在安装的时候,请选择 Elastic Stack 8.x/9.x 进行安装。在安装的时候,我们可以看到如下的安装信息:

​​

在本文中,我们使用 Elastic Stack 9.1.2 来进行展示。

启动白金试用

我们需要使用到内置的 ELSER 模型来向量化我们的数据。我们需要启动白金试用:

​​

​​

​​

​​

这样就启动了白金试用。

下载代码

我们按照如下的方式来下载代码:

bash 复制代码
`git clone https://github.com/liu-xiao-guo/transaction_search_mcp`AI写代码

我们可以看到如下的文件:

markdown 复制代码
`

1.  $ pwd
2.  /Users/liuxg/python/transaction_search_mcp
3.  $ ls -al
4.  total 32
5.  drwxr-xr-x  11 liuxg  staff   352 Feb 10 14:12 .
6.  drwxr-xr-x@ 38 liuxg  staff  1216 Feb 10 14:12 ..
7.  -rw-r--r--   1 liuxg  staff   838 Feb 10 14:12 .env.example
8.  drwxr-xr-x  12 liuxg  staff   384 Feb 10 14:12 .git
9.  -rw-r--r--   1 liuxg  staff  4050 Feb 10 14:12 .gitignore
10.  -rw-r--r--   1 liuxg  staff  4501 Feb 10 14:12 README.md
11.  drwxr-xr-x   4 liuxg  staff   128 Feb 10 14:12 docs
12.  drwxr-xr-x   4 liuxg  staff   128 Feb 10 14:12 requirements
13.  drwxr-xr-x   6 liuxg  staff   192 Feb 10 14:12 scripts
14.  drwxr-xr-x   5 liuxg  staff   160 Feb 10 14:12 src
15.  drwxr-xr-x   3 liuxg  staff    96 Feb 10 14:12 tests

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

我们把 .env.example 拷贝到 .env 文件中去:

bash 复制代码
`

1.  # Copy environment template
2.  cp .env.example .env

4.  # Edit .env with your configuration
5.  # - Elasticsearch URL and credentials
6.  # - OpenAI API key (for LLM client)

`AI写代码

.env

ini 复制代码
`

1.  # Elasticsearch Configuration
2.  ELASTICSEARCH_HOST=https://localhost:9200
3.  ELASTICSEARCH_USERNAME=elastic
4.  ELASTICSEARCH_PASSWORD=LYXAfNkSab8QwsbrriYN
5.  ELASTICSEARCH_API_KEY=X3NZeFJwd0Itb1hyWHlSejg4c3c6bHdwWGhaVTRBRTA1WnF1TUlUd1c2dw==
6.  ELASTICSEARCH_INDEX=banking_transactions

8.  OPEN_AI_KEY=<YourOpenAIKey>

`AI写代码
bash 复制代码
`

1.  transaction_search_mcp/
2.  ├── src/                          # Source code
3.  │   ├── server/                   # MCP server implementation
4.  │   │   └── server.py            # Main MCP server with transaction search tools
5.  │   └── clients/                 # Client applications
6.  │       ├── chat_client.py       # Basic Streamlit chat interface
7.  │       └── chat_client_llm.py   # Enhanced LLM-powered chat client
8.  ├── scripts/                     # Setup and utility scripts
9.  │   ├── setup_elasticsearch.py   # Basic Elasticsearch setup with test data
10.  │   ├── setup_elasticsearch_llm.py # Enhanced setup with realistic test data
11.  │   ├── run_chat.py             # Launch basic chat client
12.  │   └── run_llm_chat.py         # Launch LLM-powered chat client
13.  ├── tests/                       # Test files
14.  │   └── test_server.py          # Comprehensive server tests
15.  ├── docs/                        # Documentation
16.  │   ├── README.md               # Basic implementation docs
17.  │   └── README_LLM.md           # LLM-enhanced version docs
18.  ├── requirements/                # Dependencies
19.  │   ├── requirements.txt        # Server dependencies
20.  │   └── client_requirements.txt # Client dependencies
21.  ├── .env                        # Environment variables (not in git)
22.  ├── .env.example               # Environment template
23.  └── .gitignore                 # Git ignore rules

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

创建虚拟环境

我们使用如下的命令:

bash 复制代码
`

1.  $ python -m venv .venv
2.  $ source .venv/bin/activate
3.  (.venv) $ # Server dependencies
4.  pip install -r requirements/requirements.txt

6.  # Client dependencies (for chat interfaces)
7.  pip install -r requirements/client_requirements.txt

`AI写代码

设置 Elasticsearch

我们运行如下的命令:

bash 复制代码
`python scripts/setup_elasticsearch.py`AI写代码

​

我们可以到 Kibana 中进行查看:

​

我们可以看到有 500 个交易记录。其中的一个交易记录如下:

bash 复制代码
`GET banking_transactions/_doc/txn_000001`AI写代码
bash 复制代码
`

1.  {
2.    "_index": "banking_transactions",
3.    "_id": "txn_000001",
4.    "_version": 1,
5.    "_seq_no": 0,
6.    "_primary_term": 1,
7.    "found": true,
8.    "_source": {
9.      "transaction_id": "txn_000001",
10.      "account_id": "acc_001",
11.      "account_type": "savings",
12.      "transaction_date": "2026-01-16",
13.      "posted_date": "2026-01-17",
14.      "amount": -47.64,
15.      "currency": "USD",
16.      "description": "Home Depot - car_wash",
17.      "memo": "Purchase at Home Depot",
18.      "reference": "REF271525",
19.      "merchant": "Home Depot",
20.      "category": "gas",
21.      "subcategory": "car_wash",
22.      "transaction_type": "fee",
23.      "location": {
24.        "city": "Austin",
25.        "state": "TX",
26.        "country": "US",
27.        "postal_code": "73301",
28.        "coordinates": {
29.          "lat": 33.40455,
30.          "lon": -96.542508
31.        }
32.      },
33.      "tags": [
34.        "transportation"
35.      ],
36.      "balance_after": 3929.25,
37.      "is_pending": false,
38.      "is_recurring": false,
39.      "created_at": "2026-02-10T14:34:40.767Z",
40.      "updated_at": "2026-02-10T14:34:40.767Z"
41.    }
42.  }

`AI写代码![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

如果我们希望使用 LLM 来帮我们生成相应的模拟数据,我们可以运行:

bash 复制代码
`python scripts/setup_elasticsearch_llm.py`AI写代码

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

运行 MCP 服务器

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

vbscript 复制代码
`

1.  # Start MCP server
2.  python src/server/server.py

`AI写代码

运行客户端

我们运行如下的命令:

bash 复制代码
`python scripts/run_chat.py`AI写代码

​

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

​

​

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

bash 复制代码
`python scripts/run_llm_chat.py`AI写代码

源码请在地址 github.com/liu-xiao-gu... 下载。

相关推荐
Elasticsearch4 小时前
从 137,306 行日志定位到一个故障副本:在 Kibana 中排查错误激增
elasticsearch
Elasticsearch7 小时前
State 即将弃用,由 cpu.mode 接替:在 Elastic 中迁移 OpenTelemetry 进程指标
elasticsearch
岳涛7 小时前
考古|ES 5.3 三条改动只提前打招呼,布尔宽松写法到 6.0 才真正拒绝
elasticsearch
Elasticsearch8 小时前
谁在发起请求?为 Amazon Bedrock AgentCore 智能体配置按用户划分的 Elasticsearch 访问权限
elasticsearch
Elasticsearch9 小时前
Elasticsearch 日志写入提速:用 3 项设置和 logsdb_columnar 从每秒 5.2 万行提升到 11.2 万行
elasticsearch
Elastic 中国社区官方博客11 小时前
Elastic 获得全新的 AWS 数据能力认证
大数据·人工智能·elasticsearch·搜索引擎·ai·云计算·aws
Elasticsearch1 天前
Elastic 获得全新的 AWS 数据能力认证
elasticsearch
Elasticsearch1 天前
Elastic 安全借助 NVIDIA Nemotron 为物理隔离的安全运营中心提供智能体人工智能
elasticsearch
Elasticsearch1 天前
NVIDIA Nemotron 现已在 Elastic 推理服务(EIS)中可用
elasticsearch