如何在 vscode 里配置 MCP 并连接到 Elasticsearch

在之前的文章 "使用 MCP 将代理连接到 Elasticsearch 并对索引进行查询",我详细地描述了如何在 Claude Desktop 里配置 Elasticsearch MCP server 并连接到 Elasticsearch。对于很多的开发者来说,处于一些原因。Claude Destop 并不为我们中国开发者所能使用。在今天的文章里,我们来详述如何在 VS code 里来配置 Elasticsearch MCP server。

更多阅读:将 agents 连接到 Elasticsearch 使用模型上下文协议 - docker

安装

Elasticseach 及 Kibana

如果你还没有安装好自己的 Elasticsearch 及 Kibana,那么请你按照之前的文章 "使用 MCP 将代理连接到 Elasticsearch 并对索引进行查询" 进行安装。在这里就不一一赘述了。我们需要启动白金试用:

VS Code

我们需要针对 VS Code 来做一些安装:

  1. Github Copilot
  2. Github Copilot Chat:
  3. Gemini Code Assist:

Gemini code assist 可以轻松地帮我们在 VS code 里生成我们所需要的代码:

  • 按 Command+I 来转换你的代码并执行自定义命令
  • 在编辑器中将注释选为提示,然后按 Control+Return 生成代码
  • 点击 Activity Bar 中的 Gemini Logo () 使用 Gemini Code Assist 聊天,向它提问并改进你的代码
  • 向聊天上下文中添加文件、文件夹和工具
  • 添加自定义规则以增强聊天响应

比如,我们可以在 Github Copilot chat 里打入如下的提示:

arduino 复制代码
`我的 Elasticsearch 地址是:https://localhost:9200, 连接的 fingerprint 是:0e62f7b65c1aef92bac30593a98ae761e198514282363074326f5c81a5f341e8,超级用户 elastic 的密码是 kR8H8V3G2bgU7rvqk2rX。请创建 python 代码连接到 Elasticsearch,并使用 ping 来查看连接的状态`AI写代码

我们可以参考文章 "Elasticsearch:关于在 Python 中使用 Elasticsearch 你需要知道的一切 - 8.x" 来进行修正我们的 prompt:

arduino 复制代码
`我的 Elasticsearch 地址是:https://localhost:9200, 连接的 fingerprint 是:0e62f7b65c1aef92bac30593a98ae761e198514282363074326f5c81a5f341e8,超级用户 elastic 的密码是 kR8H8V3G2bgU7rvqk2rX。请创建 python 代码连接到 Elasticsearch,并使用 ping 来查看连接的状态。请使用 python client 版本 9.1,并使用 ssl_assert_fingerprint 来进行配置`AI写代码

很显然,这次它生成了正确的代码,并且测试正确。从这里我们可以看到 AI 生成代码的优势 :)

安装第一个 MCP 服务器

Time MCP server

我们接下来安装一个简单的 MCP 服务器来尝试一下如何在 VS Code 下来安装 MCP server。首先我们访问 MCP 服务器的官方 github 网站 github.com/modelcontex...。在这个网站中,我们可以看到已经发布的一些 MCP 服务器。我们也可以看到 Elastcsearch 也在之列。

我们先来安装一个简单的 time MCP 服务器。按照 github.com/modelcontex... 上面所示。我们可以做如下的安装步骤:

css 复制代码
 `pip install mcp-server-time`AI写代码

安装完成后,你可以通过以下方式将其作为脚本运行:

go 复制代码
`$ python -m mcp_server_time`AI写代码

这样我们的 time MCP 服务器已经运行起来了。

我们需要在 VS code 里进行一些配置:

点击 VS code 左下角的设置图标:

如上所示,在默认的情况下 chat.mcp.discovery.enabled 默认为 true。我们在这行的下面,可以针对 MCP 服务器进行配置。在 github.com/modelcontex... 点击如下的链接:

在安装上面的命令之前,我们需要安装 uv 及 uvx 包以使得安装顺利:

从上面我们可以看到有两个工具被发现。我们我们使用 Ctrl/Command + Shift + P 热键组合来进行查看:

这次,我们看到一个 time 的 MCP 服务器正在运行中:

上面显示它的完整路径:~/Library/Application\ Support/Code/User

markdown 复制代码
`

1.  $ pwd
2.  /Users/liuxg/Library/Application Support/Code/User
3.  $ cd ~/Library/Application\ Support/Code/User
4.  $ ls
5.  History          mcp.json         snippets         workspaceStorage
6.  globalStorage    settings.json    sync

`AI写代码

我们把上面的配置拷贝到 Copilot 的 Chat box 里,然后我们可以看到上面的显示结果。

如上所示,我们也可以在同样的配置文件里配置多个 MCP 服务器。我们可以在下面的 Elasticsearch 中来进行尝试。

Elasticsearch MCP server

我们来安装官方的 Elasticsearch server github.com/elastic/mcp... 来进行安装:

arduino 复制代码
`docker run docker.elastic.co/mcp/elasticsearch`AI写代码
markdown 复制代码
`

1.  $ docker run docker.elastic.co/mcp/elasticsearch
2.  Unable to find image 'docker.elastic.co/mcp/elasticsearch:latest' locally
3.  latest: Pulling from mcp/elasticsearch
4.  55d734509148: Pull complete 
5.  48ce42b85919: Pull complete 
6.  32d7fa1a9d53: Pull complete 
7.  9bc4ec11f99c: Pull complete 
8.  d9a04cb2562d: Pull complete 
9.  9985f8349d92: Pull complete 
10.  1067966164c0: Pull complete 
11.  a9652bf076e3: Pull complete 
12.  d6895a67259c: Pull complete 
13.  0e35197cc05e: Pull complete 
14.  604b5830399d: Pull complete 
15.  71f017a1c73a: Pull complete 
16.  Digest: sha256:41865d190dbdd3acf93e15eece81a60e84b9737bc36bf0b6e3c7411ee7eb2455
17.  Status: Downloaded newer image for docker.elastic.co/mcp/elasticsearch:latest
18.  Elastic MCP server

20.  Usage: elasticsearch-core-mcp-server <COMMAND>

22.  Commands:
23.    stdio  Start a stdio server
24.    http   Start a streamable-HTTP server with optional SSE support
25.    help   Print this message or the help of the given subcommand(s)

27.  Options:
28.    -h, --help  Print help

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

MCP 服务器使用以下命令以 stdio 模式启动:

bash 复制代码
`ES_URL=https://host.docker.internal:9200 ES_API_KEY=eE1WQmxKa0I5NnpZa1ZqRFZkUF86SnZoSHZNYXlheXlBX243Tk1Ja3FsZw== ES_SSL_SKIP_VERIFY=true docker run -i --rm -e ES_URL -e ES_API_KEY -e ES_SSL_SKIP_VERIFY docker.elastic.co/mcp/elasticsearch stdio`AI写代码
markdown 复制代码
`

1.  $ ES_URL=https://host.docker.internal:9200 ES_API_KEY=eE1WQmxKa0I5NnpZa1ZqRFZkUF86SnZoSHZNYXlheXlBX243Tk1Ja3FsZw== ES_SSL_SKIP_VERIFY=true docker run -i --rm -e ES_URL -e ES_API_KEY -e ES_SSL_SKIP_VERIFY docker.elastic.co/mcp/elasticsearch stdio
2.  Unable to find image 'docker.elastic.co/mcp/elasticsearch:latest' locally
3.  latest: Pulling from mcp/elasticsearch
4.  55d734509148: Pull complete 
5.  48ce42b85919: Pull complete 
6.  32d7fa1a9d53: Pull complete 
7.  9bc4ec11f99c: Pull complete 
8.  d9a04cb2562d: Pull complete 
9.  9985f8349d92: Pull complete 
10.  1067966164c0: Pull complete 
11.  a9652bf076e3: Pull complete 
12.  d6895a67259c: Pull complete 
13.  0e35197cc05e: Pull complete 
14.  604b5830399d: Pull complete 
15.  71f017a1c73a: Pull complete 
16.  Digest: sha256:41865d190dbdd3acf93e15eece81a60e84b9737bc36bf0b6e3c7411ee7eb2455
17.  Status: Downloaded newer image for docker.elastic.co/mcp/elasticsearch:latest
18.  2025-08-28T06:24:12.681905Z  INFO elasticsearch_core_mcp_server: Elasticsearch MCP server, version 0.4.5
19.  2025-08-28T06:24:12.681918Z  INFO elasticsearch_core_mcp_server: Starting stdio server
20.  time="2025-08-29T16:24:32+08:00" level=error msg="error waiting for container: unexpected EOF"
21.  $ ES_URL=https://host.docker.internal:9200 ES_API_KEY=ZWRqdDBKZ0JDUHpOTGZoR0E0UzA6Z1B2TlBpUUppTUNvUHlCWEdQSGtrdw== ES_SSL_SKIP_VERIFY=true docker run -i --rm -e ES_URL -e ES_API_KEY -e ES_SSL_SKIP_VERIFY docker.elastic.co/mcp/elasticsearch stdio
22.  2025-09-01T07:28:37.990732Z  INFO elasticsearch_core_mcp_server: Elasticsearch MCP server, version 0.4.5
23.  2025-09-01T07:28:37.990779Z  INFO elasticsearch_core_mcp_server: Starting stdio server

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

请注意:在上面,我们使用地址 host.docker.internal:9200 来代替 Elasticsearch 地址:https://localhost:9200。可以详细阅读文章 "将 agents 连接到 Elasticsearch 使用模型上下文协议 - docker"。

我们使用如下的格式来进行配置:

bash 复制代码
`

1.  {
2.    "servers": {
3.      "elasticsearch-mcp-server": {
4.        "command": "docker",
5.        "args": [
6.          "run", "-i", "--rm", 
7.          "-e", "ES_URL", 
8.          "-e", "ES_API_KEY", 
9.          "docker.elastic.co/mcp/elasticsearch", 
10.          "stdio"
11.        ],
12.        "env": {
13.          "ES_URL": "<elasticsearch-cluster-url>",
14.          "ES_API_KEY": "<elasticsearch-API-key>"
15.        }
16.      }
17.    }
18.  }

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

1.  {
2.  	"servers": {
3.  		"time": {
4.  			"command": "uvx",
5.  			"args": [
6.  				"mcp-server-time"
7.  			],
8.  			"type": "stdio"
9.  		},
10.  		"my-elasticsearch-mcp-server": {
11.  			"command": "docker",
12.  			"args": [
13.  				"run",
14.  				"-i",
15.  				"--rm",
16.  				"-e",
17.  				"ES_URL",
18.  				"-e",
19.  				"ES_API_KEY",
20.  				"-e",
21.  				"ES_SSL_SKIP_VERIFY",
22.  				"docker.elastic.co/mcp/elasticsearch",
23.  				"stdio"
24.  			],
25.  			"env": {
26.  				"ES_URL": "https://host.docker.internal:9200",
27.  				"ES_API_KEY": "eE1WQmxKa0I5NnpZa1ZqRFZkUF86SnZoSHZNYXlheXlBX243Tk1Ja3FsZw==",
28.  				"ES_SSL_SKIP_VERIFY": "true"
29.  			},
30.  			"type": "stdio"
31.  		},
32.  		"github": {
33.  			"type": "http",
34.  			"url": "https://api.githubcopilot.com/mcp/"
35.  		}
36.  	},
37.  	"inputs": []
38.  }

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

我们再次同时按下 Ctrl/Command + Shift + P:

我们之间使用 VS code 里的 Chat 功能来进行聊天:

测试 Elasticsearch MCP server:

我们按照如下的步骤来导入 Elasticsearch 索引:

我们同时打入如下的命令来创建一个叫做 people 的索引:

markdown 复制代码
`

1.  PUT /people
2.  {
3.    "mappings": {
4.      "properties": {
5.        "name": {
6.          "type": "text"
7.        },
8.        "description": {
9.          "type": "text"
10.        },
11.        "sex": {
12.          "type": "keyword"
13.        },
14.        "age": {
15.          "type": "integer"
16.        },
17.        "address": {
18.          "type": "text"
19.        }
20.      }
21.    }
22.  }

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

1.  POST /_bulk
2.  { "index" : { "_index" : "people", "_id" : "1" } }
3.  { "name" : "John Doe", "description" : "A software developer", "sex" : "Male", "age" : 30, "address" : "123 Elm Street, Springfield" }
4.  { "index" : { "_index" : "people", "_id" : "2" } }
5.  { "name" : "Jane Smith", "description" : "A project manager", "sex" : "Female", "age" : 28, "address" : "456 Maple Avenue, Anytown" }
6.  { "index" : { "_index" : "people", "_id" : "3" } }
7.  { "name" : "Alice Johnson", "description" : "A graphic designer", "sex" : "Female", "age" : 26, "address" : "789 Oak Lane, Metropolis" }
8.  { "index" : { "_index" : "people", "_id" : "4" } }
9.  { "name" : "Bob Brown", "description" : "A marketing specialist", "sex" : "Male", "age" : 32, "address" : "321 Pine Street, Gotham" }
10.  { "index" : { "_index" : "people", "_id" : "5" } }
11.  { "name" : "Charlie Davis", "description" : "An IT analyst", "sex" : "Male", "age" : 29, "address" : "654 Cedar Blvd, Star City" }
12.  { "index" : { "_index" : "people", "_id" : "6" } }
13.  { "name" : "Diana Prince", "description" : "A diplomat", "sex" : "Female", "age" : 35, "address" : "987 Birch Road, Themyscira" }
14.  { "index" : { "_index" : "people", "_id" : "7" } }
15.  { "name" : "Evan Wright", "description" : "A journalist", "sex" : "Male", "age" : 27, "address" : "213 Willow Lane, Central City" }
16.  { "index" : { "_index" : "people", "_id" : "8" } }
17.  { "name" : "Fiona Gallagher", "description" : "A nurse", "sex" : "Female", "age" : 31, "address" : "546 Spruce Street, South Side" }
18.  { "index" : { "_index" : "people", "_id" : "9" } }
19.  { "name" : "George King", "description" : "A teacher", "sex" : "Male", "age" : 34, "address" : "879 Elm St, Smallville" }
20.  { "index" : { "_index" : "people", "_id" : "10" } }
21.  { "name" : "Helen Parr", "description" : "A full-time superhero", "sex" : "Female", "age" : 37, "address" : "123 Metro Avenue, Metroville" }

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

在上面的例子中,我们可以看到有5个男的5个女的。我们打入如下的问题:

sql 复制代码
`How many are males and how many are females?`AI写代码
vbnet 复制代码
`Please use index kibana_sample_data_flights to find out the cheapest price from CN to US and tell me the OriginCityName and DestCityName`AI写代码
go 复制代码
`请使用索引 kibana_sample_data_flights 查找从 中国 到 美国 的最便宜价格,并告诉我 OriginCityName 和 DestCityName`AI写代码

祝大家使用 VS code 顺序!

相关推荐
在钱塘江3 小时前
Elasticsearch 快速入门 - Python版本
后端·python·elasticsearch
斯普信专业组7 小时前
Filebeat写ElasticSearch故障排查思路(下)
运维·elasticsearch·中间件·filebeat
伊织code1 天前
Elasticsearch - 分布式搜索与分析引擎
大数据·分布式·elasticsearch
小松XXS1 天前
elasticsearch面试八股文
大数据·elasticsearch·面试
risc1234561 天前
【elasticsearch】序列化与反序列化机制
elasticsearch
Elasticsearch1 天前
Elastic AI agent builder 介绍
elasticsearch
Elasticsearch1 天前
MCP 工具:自主代理的攻击向量与防御建议
elasticsearch
和光同尘20231 天前
CentOS7搭建ELK日志分析系统
运维·elasticsearch·云原生·kubernetes·centos·kibana·logstash
Elastic 中国社区官方博客1 天前
理解 Elasticsearch 中的分块策略
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索