0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MCPサーバーを実装してCursorエディタで叩いてみる

Last updated at Posted at 2025-04-13

はじめに

今回はMCPサーバーを実装して、Cursorエディタのチャットからアクセスするまでの手順を紹介します。

事前準備

プロジェクトを作成します。

mkdir mcp-test
code mcp-test
npm init

今回使用するライブラリをインストールします

npm i npm install @modelcontextprotocol/sdk  

実装

クイックスタートのコードをそのまま持ってきます。
server.resourceにつづく部分は、現在Cursorエディタからresourceが参照できないため削除します。(残していても問題はないです)

index.js
import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";

// Create an MCP server
const server = new McpServer({
  name: "Demo",
  version: "1.0.0"
});

// Add an addition tool
server.tool("add",
  { a: z.number(), b: z.number() },
  async ({ a, b }) => ({
    content: [{ type: "text", text: String(a + b) }]
  })
);

// Start receiving messages on stdin and sending messages on stdout
const transport = new StdioServerTransport();
await server.connect(transport);

package.jsonに追記します。

{
  "name": "mcp-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module", // <--追記
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@modelcontextprotocol/sdk": "^1.9.0"
  }
}

MCPサーバーをCursorに設定

基本設定 → Cursor Setting → MCP → Add new Global MCP serverを選択、
mcp.jsonに以下を追記します。(argsは前述したindex.jsのフルパスを記載してください)

{
  "mcpServers": {
    "Demo": {
      "command": "node",
      "args": ["index.jsのパスを記載"]
    }
  }
}

基本設定 → Cursor Setting → MCPにもどって、以下のスクショのように緑色のランプが付いていたら問題ないです。

スクリーンショット 2025-04-13 23.57.45.png

Chatから叩く

テキストで質問して見ます。

スクリーンショット 2025-04-13 23.59.14.png

addというtoolを参照して、きちんと返答が行われました!

toolを追加してみる

toolは複数定義できます。
試しに、uuidを生成するtoolを定義して見ましょう。

index.js
// uuidを生成するtool
server.tool("generate_uuid", {}, async () => ({
  content: [{ type: "text", text: crypto.randomUUID() }],
}));

スクリーンショット 2025-04-14 0.01.22.png

refreshボタンを押すと、toolの部分にgenerate_uuidが追加されているのが確認できます。

uuidを生成してもらいましょう。

スクリーンショット 2025-04-14 0.03.02.png

正常に生成できたようです!

参考

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?