ソースを参照

feat: add GOGS_REPO environment variable for repository access restriction #20

Implement repository-scoped access control through GOGS_REPO environment variable:

- Add GOGS_REPO env var validation and parsing in src/index.ts
- Create helper functions createRepoSchemaFields() and resolveRepoParams()
- Convert all repository-specific tool schemas to dynamic builders
- Make owner/repo parameters optional when GOGS_REPO is set
- Update all tool handlers to use resolveRepoParams() for validation
- Enforce access restrictions: block access to other repos when GOGS_REPO is set
- Extend tool descriptions with repository context when restricted
- Document GOGS_REPO in .env.example

When GOGS_REPO is set to "owner/repo":
- Tool schemas mark owner/repo as optional
- Handlers automatically use GOGS_REPO values when params not provided
- Access to other repositories is denied with clear error messages
- Tool descriptions show "(restricted to owner/repo)" context

This enables secure, single-repository access for Claude Code deployments.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
claude 8 ヶ月 前
コミット
7a76edb287
4 ファイル変更259 行追加201 行削除
  1. 5 0
      .env.example
  2. 1 1
      .mcp-gogs.json
  3. 17 1
      src/index.ts
  4. 236 199
      src/server.ts

+ 5 - 0
.env.example

@@ -7,6 +7,11 @@ GOGS_SERVER_URL=https://your-gogs-server.com
 # Generate this from your Gogs instance: Settings → Applications → Generate New Token
 GOGS_ACCESS_TOKEN=your-access-token-here
 
+# Optional: Restrict access to a specific repository (format: owner/repo)
+# When set, only this repository can be accessed and repo parameters become optional
+# Example: GOGS_REPO=myuser/myproject
+# GOGS_REPO=
+
 # Transport mode: 'stdio' (default) or 'http'
 # - stdio: For use with MCP clients (Claude Desktop, etc.)
 # - http: For HTTP-based access with SSE

+ 1 - 1
.mcp-gogs.json

@@ -4,7 +4,7 @@
       "type": "stdio",
       "command": "node",
       "args": [
-        ""
+        "/home/claude/gogs-mcp/dist/index.js"
       ],
       "env": {}
     }

+ 17 - 1
src/index.ts

@@ -18,6 +18,7 @@ config();
 // Get configuration from environment variables
 const GOGS_SERVER_URL = process.env.GOGS_SERVER_URL;
 const GOGS_ACCESS_TOKEN = process.env.GOGS_ACCESS_TOKEN;
+const GOGS_REPO = process.env.GOGS_REPO;
 const TRANSPORT_MODE = process.env.TRANSPORT_MODE || 'stdio';
 const HTTP_PORT = parseInt(process.env.HTTP_PORT || '3000', 10);
 const HTTP_HOST = process.env.HTTP_HOST || '0.0.0.0';
@@ -27,6 +28,21 @@ if (!GOGS_SERVER_URL) {
   process.exit(1);
 }
 
+// Validate GOGS_REPO format if set
+if (GOGS_REPO && !GOGS_REPO.includes('/')) {
+  console.error('Error: GOGS_REPO must be in format "owner/repo"');
+  process.exit(1);
+}
+
+// Parse GOGS_REPO into owner and repo
+let repoOwner: string | undefined;
+let repoName: string | undefined;
+if (GOGS_REPO) {
+  const parts = GOGS_REPO.split('/');
+  repoOwner = parts[0];
+  repoName = parts[1];
+}
+
 // Initialize Gogs client
 const gogsClient = new GogsClient({
   serverUrl: GOGS_SERVER_URL,
@@ -34,7 +50,7 @@ const gogsClient = new GogsClient({
 });
 
 // Create MCP server with all tools and handlers
-const server = createMcpServer(gogsClient);
+const server = createMcpServer(gogsClient, repoOwner, repoName);
 
 // Store http server instance for cleanup
 let httpServer: HttpMcpServer | null = null;

ファイルの差分が大きいため隠しています
+ 236 - 199
src/server.ts


この差分においてかなりの量のファイルが変更されているため、一部のファイルを表示していません