Brettapps commited on
Commit
98407a1
·
verified ·
1 Parent(s): f1d8c1b

Upload folder using huggingface_hub

Browse files
Files changed (6) hide show
  1. Dockerfile +10 -0
  2. README.md +16 -5
  3. __init__.py +0 -0
  4. hf_secrets.py +32 -0
  5. pyproject.toml +18 -0
  6. server.py +70 -0
Dockerfile ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ WORKDIR /app
4
+
5
+ COPY pyproject.toml .
6
+ RUN pip install .
7
+
8
+ COPY . .
9
+
10
+ CMD ["python", "server.py"]
README.md CHANGED
@@ -1,10 +1,21 @@
1
  ---
2
- title: Secrets Manager Mcp
3
- emoji: 📊
4
- colorFrom: indigo
5
- colorTo: pink
6
  sdk: docker
7
  pinned: false
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Secrets-Manager-MCP
3
+ emoji: 🔐
4
+ colorFrom: red
5
+ colorTo: yellow
6
  sdk: docker
7
  pinned: false
8
+ app_port: 8000
9
  ---
10
 
11
+ # Secrets Manager MCP Server
12
+
13
+ This agent automates the management of secrets (environment variables) for Hugging Face Spaces. It allows you to programmatically add, delete, and list secrets.
14
+
15
+ ## Features
16
+ - **Add Secret**: Securely push environment variables to any Space.
17
+ - **Configure Agent**: Quick-start tool for setting up OpenAI and Stripe keys.
18
+ - **List Secrets**: Audit existing secret keys in your Spaces.
19
+
20
+ ## Deployment
21
+ This Space is configured to run the MCP server.
__init__.py ADDED
File without changes
hf_secrets.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from typing import List, Optional
3
+ from huggingface_hub import HfApi
4
+
5
+ class HFSecretsManager:
6
+ def __init__(self, token: Optional[str] = None):
7
+ self.api = HfApi(token=token or os.getenv("HF_TOKEN"))
8
+
9
+ def add_secret(self, repo_id: str, secret_name: str, secret_value: str) -> str:
10
+ """Adds or updates a secret in a Hugging Face Space."""
11
+ try:
12
+ self.api.add_space_secret(repo_id=repo_id, key=secret_name, value=secret_value)
13
+ return f"Successfully added secret '{secret_name}' to {repo_id}."
14
+ except Exception as e:
15
+ return f"Error adding secret: {str(e)}"
16
+
17
+ def delete_secret(self, repo_id: str, secret_name: str) -> str:
18
+ """Deletes a secret from a Hugging Face Space."""
19
+ try:
20
+ self.api.delete_space_secret(repo_id=repo_id, key=secret_name)
21
+ return f"Successfully deleted secret '{secret_name}' from {repo_id}."
22
+ except Exception as e:
23
+ return f"Error deleting secret: {str(e)}"
24
+
25
+ def list_secrets(self, repo_id: str) -> List[str]:
26
+ """Lists the names of secrets in a Hugging Face Space."""
27
+ try:
28
+ # list_space_secrets returns a list of Secret objects (which only show keys)
29
+ secrets = self.api.list_space_secrets(repo_id=repo_id)
30
+ return [s.key for s in secrets]
31
+ except Exception as e:
32
+ return [f"Error listing secrets: {str(e)}"]
pyproject.toml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "secrets-manager"
3
+ version = "0.1.0"
4
+ description = "Secrets Manager MCP Server Agent for HF Spaces"
5
+ readme = "README.md"
6
+ requires-python = ">=3.10"
7
+ dependencies = [
8
+ "mcp[cli]>=1.21.2",
9
+ "huggingface_hub>=0.24.0",
10
+ "python-dotenv>=1.0.0",
11
+ ]
12
+
13
+ [build-system]
14
+ requires = ["setuptools>=61.0"]
15
+ build-backend = "setuptools.build_meta"
16
+
17
+ [project.scripts]
18
+ secrets-manager = "secrets_manager.server:main"
server.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from mcp.server.fastmcp import FastMCP
2
+ from secrets_manager.hf_secrets import HFSecretsManager
3
+ import os
4
+ from dotenv import load_dotenv
5
+ from typing import List, Optional
6
+
7
+ # Load environment variables
8
+ load_dotenv()
9
+
10
+ # Initialize FastMCP
11
+ mcp = FastMCP("Secrets-Manager")
12
+
13
+ # Initialize Secrets Manager
14
+ secrets_mgr = HFSecretsManager()
15
+
16
+ @mcp.tool()
17
+ def add_space_secret(repo_id: str, secret_name: str, secret_value: str) -> str:
18
+ """
19
+ Adds or updates a secret (environment variable) in a Hugging Face Space.
20
+
21
+ Args:
22
+ repo_id: The full Space ID (e.g., 'username/space-name').
23
+ secret_name: The name of the secret (e.g., 'OPENAI_API_KEY').
24
+ secret_value: The value of the secret.
25
+ """
26
+ # Note: FastMCP logs tool calls, but we should be careful about printing values.
27
+ # The return message is safe.
28
+ return secrets_mgr.add_secret(repo_id, secret_name, secret_value)
29
+
30
+ @mcp.tool()
31
+ def delete_space_secret(repo_id: str, secret_name: str) -> str:
32
+ """
33
+ Deletes a secret from a Hugging Face Space.
34
+ """
35
+ return secrets_mgr.delete_secret(repo_id, secret_name)
36
+
37
+ @mcp.tool()
38
+ def list_space_secrets(repo_id: str) -> str:
39
+ """
40
+ Lists the names of all secrets currently configured in a Hugging Face Space.
41
+ Note: Values are hidden by Hugging Face.
42
+ """
43
+ keys = secrets_mgr.list_secrets(repo_id)
44
+ if not keys:
45
+ return f"No secrets found in {repo_id}."
46
+ return f"Secrets in {repo_id}:\n" + "\n".join([f"- {k}" for k in keys])
47
+
48
+ @mcp.tool()
49
+ def configure_agent_space(repo_id: str, openai_key: Optional[str] = None, stripe_key: Optional[str] = None, hf_token: Optional[str] = None) -> str:
50
+ """
51
+ Helper tool to quickly configure a new agent Space with necessary secrets.
52
+ """
53
+ results = []
54
+ if openai_key:
55
+ results.append(secrets_mgr.add_secret(repo_id, "OPENAI_API_KEY", openai_key))
56
+ if stripe_key:
57
+ results.append(secrets_mgr.add_secret(repo_id, "STRIPE_SECRET_KEY", stripe_key))
58
+ if hf_token:
59
+ results.append(secrets_mgr.add_secret(repo_id, "HF_TOKEN", hf_token))
60
+
61
+ if not results:
62
+ return "No keys provided to configure."
63
+
64
+ return "Configuration Summary:\n" + "\n".join(results)
65
+
66
+ def main():
67
+ mcp.run()
68
+
69
+ if __name__ == "__main__":
70
+ main()