0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Cursorのmcp.jsonでプロジェクトルートにある.envを読み込んでMCPサーバーを起動する

Posted at

はじめに

CursorとNotionを上手く連携させたいと思い、Notion MCPサーバーをCursorで活用してみている。CusorにてNotionのMCPサーバーの設定する際は、Notion MCP サーバーの公式GitHub (https://github.com/makenotion/notion-mcp-server) を参考に、 .cursor/mcp.jsonを設置して、mcp.json 内にある "OPENAPI_MCP_HEADERS": "{\"Authorization\": \"Bearer ntn_****\", \"Notion-Version\": \"2022-06-28\" }"ntn_**** に自身のNotionワークスペースのAPIキーを書き込むだけだ。

しかし、例えばこのmcp.jsonをgit管理したい時、APIキーをハードコーディングしたくないと考える。そこで、.env などを作成し、.gitignore でgit管理から外したところに記載されたAPIキーをmcp.json にて呼び出したいと思うはず。Cursorでこれを実現する際に、少し手こずってしまったので同じことで悩んでいる人のために共有する。また、より楽でシンプルな解決策があれば教えていただきたい。

要約

以下のようなディレクトリ構成の時、

❯ tree -a
.
├── .cursor
│   └── mcp.json
├── .env
├── .gitignore
└── start-mcp-docker.sh

mcp.json

{
  "mcpServers": {
    "notionApi": {
      "command": "./start-mcp-docker.sh",
      "args": []
    }
  }
}

.env

OPENAPI_MCP_HEADERS={"Authorization": "Bearer <Your Notion API Key>", "Notion-Version": "2022-06-28"}

start-mcp-docker.sh

#!/bin/bash

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE:-$0}")" && pwd)"

if [ ! -f "$SCRIPT_DIR/.env" ]; then
  echo "Error: .env file not found in $SCRIPT_DIR" >&2
  exit 1
fi

docker run --rm -i --env-file "$SCRIPT_DIR/.env" mcp/notion

とすることで、.envからOPENAPI_MCP_HEADERSを読み込んで、正しくNotion MCPサーバーを起動できる。

説明

もっと楽な方法があるだろうと色々試していたが、上記のやり方に辿り着いてしまった。

実際に、mcp.jsonに秘匿するべき情報をハードコーディングしたくないという要望はCursorコミュニティでもいくつか上がっている (https://forum.cursor.com/t/how-to-use-environment-variables-in-mcp-json/79296/9, https://forum.cursor.com/t/mcp-json-pass-secrets-securely/72942, https://forum.cursor.com/t/resolve-local-environment-variables-in-mcp-server-definitions/79639) が、現状まだ良い呼び出し方が実装されたわけではなさそうだ。

また、ユーザーの環境の環境変数も引き継がないようなので、Roo Codeのような方法 (https://zenn.dev/budougumi617/articles/roo-can-inject-env-in-mcp-json) でMCPサーバー起動前に設定した環境変数を呼び出すといったこともできなさそう。

加えて、npxを用いる際はdotenv-cli、Dockerを用いる際は--env-fileオプションを用いて、.envを参照して環境変数の設定も可能だが、MCPサーバーはデフォルトでシステムのルートにて実行されるようなので、git管理されているプロジェクトのディレクトリまでのパスをmcp.jsonに書かなければならなくなる。

実際に下記のようなスクリプトをmcp.jsonに設定して、

#!/bin/bash

echo "cwd is: $(pwd)" >&2

Cursorを再起動時にMCP Logsをターミナルから確認すると、

2025-07-06 00:35:39.758 [info] project-0-cursor-env-notionApi: Handling CreateClient action
2025-07-06 00:35:39.758 [info] project-0-cursor-env-notionApi: Starting new stdio process with command: /Users/.../demo.sh
2025-07-06 00:35:39.758 [info] project-0-cursor-env-notionApi: Handling ListOfferings action
2025-07-06 00:35:39.758 [error] project-0-cursor-env-notionApi: No server info found
2025-07-06 00:35:40.057 [error] project-0-cursor-env-notionApi: cwd is: /

2025-07-06 00:35:40.066 [info] project-0-cursor-env-notionApi: Client closed for command
2025-07-06 00:35:40.087 [info] project-0-cursor-env-notionApi: Handling ListOfferings action
2025-07-06 00:35:40.087 [error] project-0-cursor-env-notionApi: No server info found
2025-07-06 00:35:40.088 [info] project-0-cursor-env-notionApi: Handling ListOfferings action
2025-07-06 00:35:40.088 [error] project-0-cursor-env-notionApi: No server info found

cwd is: /と確かに表示される。

さらに、cwdプロパティでコマンドが実行されるディレクトリを指定することも可能だが、その場合も同様にgit管理されたプロジェクトのあるパスをハードコーディングしなければならない。

そこで、npxまたはdockerコマンドを実行する部分をシェルスクリプトでラッピングし、このシェルスクリプトでプロジェクトのディレクトリ配下にある.envを読み込んで、MCPサーバー起動時に渡してあげるようにする。

これを可能にするための核となるのは、SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE:-$0}")" && pwd)"という部分であり、これによって、シェルスクリプトが設置されているディレクトリのパス=プロジェクトディレクトリのパスを取得可能となる。

そのあとは、(例えばDockerを使用する場合) 下記のように.envへのパスをMCPサーバー起動時に渡してあげるだけで良い。

docker run --rm -i --env-file "$SCRIPT_DIR/.env" mcp/notion 

手元で試した際は、確かに問題なく動いてそうだったのでこれで良さそう。

2025-07-06 00:54:23.837 [info] project-0-cursor-env-notionApi: Handling CreateClient action
2025-07-06 00:54:23.837 [info] project-0-cursor-env-notionApi: Starting new stdio process with command: /Users/.../cursor-env/start-mcp-docker.sh
2025-07-06 00:54:24.607 [info] project-0-cursor-env-notionApi: Successfully connected to stdio server
2025-07-06 00:54:24.607 [info] project-0-cursor-env-notionApi: Storing stdio client
2025-07-06 00:54:24.702 [info] project-0-cursor-env-notionApi: Handling ListOfferings action
2025-07-06 00:54:24.702 [info] project-0-cursor-env-notionApi: Listing offerings
2025-07-06 00:54:24.702 [info] project-0-cursor-env-notionApi: Connected to stdio server, fetching offerings
2025-07-06 00:54:24.702 [info] project-0-cursor-env-notionApi: Handling ListOfferings action
2025-07-06 00:54:24.702 [info] project-0-cursor-env-notionApi: Listing offerings
2025-07-06 00:54:24.702 [info] project-0-cursor-env-notionApi: Connected to stdio server, fetching offerings
2025-07-06 00:54:24.836 [info] listOfferings: Found 19 tools
2025-07-06 00:54:24.836 [info] project-0-cursor-env-notionApi: Found 19 tools
2

スクリーンショット 2025-07-06 0.59.50.png

最終形は既に「要約」にて示した通りである。

おわりに

安全に秘匿情報をMCPサーバーへ渡せるような仕組みは公式がいつか公開すると思うので、それまでの一時的な対応になると思う。なんなら、Cursorが廃れることもあり得るし。

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?