mcpField
A
AI Agent with MCP

AI Agent with MCP

本地服务
github.com/moises-paschoalickAI 与智能体270 周前更新

关于 AI Agent with MCP

AI Agent with MCP 是一个 Node.js 项目,展示 AI Agent 如何通过 MCP 使用外部资源。项目包含用户和问候语资源、MCP 客户端,以及通过终端、REST API 和 Cursor 访问的示例。

主要功能

  1. 01提供用户和问候语资源的 MCP 服务
  2. 02用于访问资源的 MCP 客户端
  3. 03通过 REST API 直接访问
  4. 04终端和 Cursor 使用示例

适用场景

  1. 01获取已登记的用户列表
  2. 02读取问候语资源
  3. 03通过 Cursor 或终端探索 MCP 访问方式

MCP 相关

项目提供 MCP 服务以公开用户和问候语资源,并包含用于调用这些资源的 MCP 客户端。还展示了通过 REST API 直接访问的方式。

配置

本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。

mcp.json · 通用配置
{
"mcpServers": {
"ai-agent-with": {
"command": "npx",
"args": [
"-y",
"@smithery/cli"
]
}
}
}
启动命令npx -y @smithery/cli

本地服务需要在你的电脑上安装运行环境(如 Node.js 18+)。各客户端的配置文件位置不同,请参考客户端文档。

安装包
npm@smithery/cli

工具

还没有获取到工具列表

本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。

详细介绍

使用 NodeJS 创建 MCP server(ai-agent-with-mcp)

此项目实现了一个 AI Agent,使用 Model Context Protocol(MCP)与外部资源交互。

仓库包含:

一个可运行的 MCP 服务,提供用户和消息的 REST 资源。

一个用于通过协议使用这些资源的 MCP 客户端。

一些实用示例,展示如何通过终端、REST API 或直接在 Cursor 中访问这些资源。

可用资源

MCP 服务提供以下资源:

用户资源
  • api://users:列出系统中注册的所有用户
    • 返回 ID、姓名、电话号码和 thread ID 等信息
    • 格式:JSON
    • 响应示例:
      {
        "content": [
          {
            "id": 1,
            "phoneNumber": "553496341404",
            "name": "Moisés Paschoalick",
            "threadId": "thread_bZEPVYVBvHxY9Ok6WqR63M2D"
          },
          {
            "id": 2,
            "phoneNumber": "553496338888",
            "name": "José Silva",
            "threadId": "1thread_t8SFjKio6yN9pppqypilwGoR__"
          },
          {
            "id": 3,
            "phoneNumber": "553496338889",
            "name": "Maria Antonia",
            "threadId": "1thread_t8SFjKio6yN9pppqypilwGoR__"
          }
        ],
        "pageable": {
          "pageNumber": 0,
          "pageSize": 12,
          "sort": {
            "empty": false,
            "sorted": true,
            "unsorted": false
          },
          "offset": 0,
          "paged": true,
          "unpaged": false
        },
        "last": true,
        "totalPages": 1,
        "totalElements": 3,
        "first": true,
        "size": 12,
        "number": 0,
        "sort": {
          "empty": false,
          "sorted": true,
          "unsorted": false
        },
        "numberOfElements": 3,
        "empty": false
      }
      
消息资源
  • hello://world:返回一条简单的问候消息
    • 格式:纯文本(text/plain)
    • 响应示例:"Hello, World! This is my first MCP resource."

在 Cursor 中安装

首先,请确保已安装 Cursor,并且已在系统中配置 npm。

选项 1:通过终端安装

在终端中运行以下命令:

npx -y @smithery/cli@latest install @wonderwhy-er/desktop-commander --client cursor --key dda23bec-caa6-4487-a1e9-eb74e22e33eb

注意:提到的 key 仅为示例,你需要添加自己的 key。

如果 Cursor 正在运行,请重启。

选项 2:手动安装

将相应条目添加到你的 mcp.json 文件中:

Mac/Linux:

Linux 上:~/.config/cursor.json

{
  "mcpServers": {
    "desktop-commander": {
      "command": "npx",
      "args": [
        "-y",
        "@smithery/cli@latest",
        "run",
        "@wonderwhy-er/desktop-commander",
        "--key",
        "dda23bec-caa6-4487-a1e9-eb74e22e33eb"
      ]
    }
  }
}
Windows:
{
  "mcpServers": {
    "desktop-commander": {
      "command": "cmd",
      "args": [
        "/c",
        "npx",
        "-y",
        "@smithery/cli@latest",
        "run",
        "@wonderwhy-er/desktop-commander",
        "--key",
        "dda23bec-caa6-4487-a1e9-eb74e22e33eb"
      ]
    }
  }
}

如果 Cursor 正在运行,请重启。

选项 3:本地克隆

克隆并构建:

git clone https://github.com/moises-paschoalick/ai-agent-with-mcp.git
cd ai-agent-with-mcp
npm run setup

如果 Cursor 正在运行,请重启。

配置命令将:

  • 安装依赖项
  • 构建服务
  • 配置 Cursor
  • 在需要时将 MCP 服务添加到 Cursor 配置中

使用方法

MCP 客户端

此项目包含一个可用于访问可用资源的 MCP 客户端:

import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";

async function main() {
  const client = new Client({
    name: "hello-mcp-client",
    version: "1.0.0"
  });
  const transport = new StdioClientTransport({
    command: "node",
    args: ["build/index.js"]
  });
  
  try {
    await client.connect(transport);
    
    // Listar recursos disponíveis
    const resources = await client.listResources();
    console.log("Recursos disponíveis:", resources);
    
    // Ler o recurso de usuários
    const content = await client.readResource({ uri: "api://users" });
    console.log("\nLista de Usuários:", content);
  } catch (error) {
    console.error("Erro ao executar o cliente:", error);
  }
}

main().catch(console.error);
直接使用 REST API

你也可以直接通过 REST API 访问这些资源:

# Listar todos os usuários
curl http://3.238.149.189:8080/users

安装和运行

# Instalar dependências
npm install

# Compilar o projeto
npm run build

# Executar o servidor
npm start

# Executar em modo de desenvolvimento
npm run dev

要求

  • Node.js(版本需兼容 AbortController)
  • TypeScript
  • @modelcontextprotocol/sdk

评论

支持 Markdown

第一次接触 MCP?

从协议是什么,到在客户端里装上第一个服务,这几条回答覆盖了大多数入门问题。

一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。