mcp-cos-upload 1.1.4 → 1.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +3 -1
  2. package/package.json +1 -1
  3. package/src/cos.js +50 -5
package/README.md CHANGED
@@ -72,7 +72,7 @@ COS_CDN_DOMAIN=cdn.example.com
72
72
  > **Note:** The `.env` file contains secrets — never commit it. Use `.env.example` as a reference template.
73
73
 
74
74
  The server only reads one file:
75
- 1. `process.cwd()/.env`
75
+ 1. `<project-root>/.env`
76
76
 
77
77
  It does **not** read:
78
78
  1. MCP `env` configuration
@@ -80,6 +80,8 @@ It does **not** read:
80
80
  3. Parent directory `.env`
81
81
  4. `COS_ENV_PATH`
82
82
 
83
+ Project root is inferred from the current working directory by walking upward and finding common project markers such as `.git`, `package.json`, `pnpm-workspace.yaml`, `.cursor`, or `.mcp.json`. After the root is determined, only that single `.env` file is read.
84
+
83
85
  ### Environment Variables
84
86
 
85
87
  | Variable | Required | Description |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mcp-cos-upload",
3
- "version": "1.1.4",
3
+ "version": "1.1.5",
4
4
  "description": "MCP server for uploading files to Tencent Cloud COS",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/cos.js CHANGED
@@ -1,9 +1,52 @@
1
1
  // mcp-cos-upload - COS client configuration
2
2
  import COS from "cos-nodejs-sdk-v5";
3
3
  import { readFileSync, existsSync } from "fs";
4
- import { join } from "path";
4
+ import { dirname, join, resolve } from "path";
5
5
 
6
- const PROJECT_ENV_PATH = join(process.cwd(), ".env");
6
+ const REPO_MARKERS = [".git", ".hg", ".svn"];
7
+ const PROJECT_MARKERS = [
8
+ ".mcp.json",
9
+ ".cursor",
10
+ ".claude",
11
+ "package.json",
12
+ "pnpm-workspace.yaml",
13
+ "turbo.json",
14
+ "lerna.json",
15
+ "nx.json",
16
+ "pyproject.toml",
17
+ "Cargo.toml",
18
+ "go.mod",
19
+ ];
20
+
21
+ function hasAnyMarker(dirPath, markerNames) {
22
+ return markerNames.some((markerName) => existsSync(join(dirPath, markerName)));
23
+ }
24
+
25
+ function findProjectRoot(startDir) {
26
+ let currentDir = resolve(startDir);
27
+ let repoRoot = null;
28
+ let projectRoot = null;
29
+
30
+ while (true) {
31
+ if (hasAnyMarker(currentDir, REPO_MARKERS)) {
32
+ repoRoot = currentDir;
33
+ }
34
+
35
+ if (hasAnyMarker(currentDir, PROJECT_MARKERS)) {
36
+ projectRoot = currentDir;
37
+ }
38
+
39
+ const parentDir = dirname(currentDir);
40
+ if (parentDir === currentDir) break;
41
+ currentDir = parentDir;
42
+ }
43
+
44
+ return repoRoot || projectRoot || resolve(startDir);
45
+ }
46
+
47
+ const CURRENT_WORKING_DIRECTORY = process.cwd();
48
+ const PROJECT_ROOT = findProjectRoot(CURRENT_WORKING_DIRECTORY);
49
+ const PROJECT_ENV_PATH = join(PROJECT_ROOT, ".env");
7
50
 
8
51
  function parseEnvFile(filePath) {
9
52
  const env = {};
@@ -32,8 +75,9 @@ function getMissingCredentialMessage() {
32
75
  if (!fileEnv) {
33
76
  return [
34
77
  "[mcp-cos-upload] ❌ 未找到配置文件",
35
- `只会读取项目根目录 .env: ${PROJECT_ENV_PATH}`,
36
- `当前工作目录: ${process.cwd()}`,
78
+ `项目根目录: ${PROJECT_ROOT}`,
79
+ `只会读取这个文件: ${PROJECT_ENV_PATH}`,
80
+ `当前工作目录: ${CURRENT_WORKING_DIRECTORY}`,
37
81
  "请在该文件中设置:",
38
82
  "COS_SECRET_ID=your_secret_id",
39
83
  "COS_SECRET_KEY=your_secret_key",
@@ -44,8 +88,9 @@ function getMissingCredentialMessage() {
44
88
 
45
89
  return [
46
90
  "[mcp-cos-upload] ❌ .env 缺少 COS_SECRET_ID 或 COS_SECRET_KEY",
91
+ `项目根目录: ${PROJECT_ROOT}`,
47
92
  `只读取此文件: ${PROJECT_ENV_PATH}`,
48
- `当前工作目录: ${process.cwd()}`,
93
+ `当前工作目录: ${CURRENT_WORKING_DIRECTORY}`,
49
94
  "请补全以下字段:",
50
95
  "COS_SECRET_ID=your_secret_id",
51
96
  "COS_SECRET_KEY=your_secret_key",