Back to MCP directory
publicPublicdnsLocal runtime

cosmosdb-mcp

该项目展示如何为Azure Cosmos DB创建MCP服务器和客户端,包含前端应用(产品目录和AI助手)和连接数据库的MCP服务器组件。

article

README

🚀 Azure Cosmos DB 入门指南

本指南将帮助你快速上手 Azure Cosmos DB,介绍环境准备、快速入门步骤、操作指南以及相关参考资源,助你顺利使用 Azure Cosmos DB 进行开发。

🚀 快速开始

要使用 Azure Cosmos DB,需要完成环境准备和资源创建等步骤,以下是详细流程。

📦 安装指南

安装必要的工具

  • Node.js 和 npm:确保安装了 Node.js 和 npm(版本 14 或更高),你可以从 Node.js 官网 下载。
  • .NET SDK:安装 .NET SDK,用于构建和运行 ASP.NET Core 应用程序,可从 微软官网 下载。
  • Azure CLI:安装 Azure CLI,用于与 Azure 服务交互,可从 Azure 官网 下载。

创建 Azure 账户

如果你还没有 Azure 账户,请先注册一个免费的试用账户:Azure 免费试用

💻 使用示例

创建资源组和存储帐户

  1. 登录到 Azure 门户
  2. 在左侧菜单中选择 “创建资源组”,输入名称并创建新的资源组。
  3. 在资源组中,选择 “创建存储帐户”,按照向导配置存储帐户的参数(如名称、区域等)。

配置 Cosmos DB

  1. 在 Azure 门户中,选择 “创建数据库服务” 并选择 “Cosmos DB for NoSQL”
  2. 输入必要的信息(如名称、资源组、位置),然后完成创建。

下载并配置代码示例

基础用法

# 克隆提供的代码仓库
git clone <repository-url>

在项目根目录中,复制 .env.template 文件为 .env,并根据实际情况修改配置:

{
  "CosmosDb": {
    "Endpoint": "https://<cosmosdb_account_name>.documents.azure.com:443/",
    "TenantId": "<tenant_id>",
    "DatabaseName": "eshop",
    "ProductsContainerName": "products",
    "OrdersContainerName": "orders"
  }
}

运行应用程序

# 构建 Web API
cd webapi
dotnet build

# 启动 Web API
cd webapi
dotnet run

🔧 操作指南

数据填充

修改 appsettings.json 文件,替换占位符为实际值:

{
  "CosmosDb": {
    "Endpoint": "https://<cosmosdb_account_name>.documents.azure.com:443/",
    "TenantId": "<tenant_id>",
    "DatabaseName": "eshop",
    "ProductsContainerName": "products",
    "OrdersContainerName": "orders"
  }
}

在终端中运行以下命令以填充产品目录:

cd populate
az login
dotnet run

实验步骤

启动服务

# 启动 MCP 服务器
cd mcp-server
npx ts-node src/server.ts

# 启动前端项目(storefront)
cd nextjs
npm start

演示步骤

  1. 打开浏览器,访问 http://localhost:3002
  2. 点击右上角的 “AI 助手” 图标。
  3. 输入 "I'm interested in backpacks"(产品列表将刷新为背包列表)。
  4. 输入 "Get my orders"(订单列表将刷新显示订单信息)。

演示

📚 参考资源

help

Runtime guide

cloud

Hosted runtime

Hosted servers run from a provider-managed environment. You usually connect the MCP client to the hosted endpoint or follow the provider's authorization flow, without keeping a local process alive

  1. Open provider connection page
  2. Authorize or copy endpoint
  3. Connect from your MCP client
terminal

Local runtime / other methods

Local servers run on your own machine or infrastructure. You normally copy the server_config into your MCP client, install the required package, and provide env variables from env_schema when needed

  1. Copy server_config
  2. Install required package
  3. Fill env variables and restart client