Skip to content

Commit cffd8e8

Browse files
committed
Mcp samples for env server and long running
1 parent d9005f7 commit cffd8e8

File tree

11 files changed

+2841
-0
lines changed

11 files changed

+2841
-0
lines changed

samples/mcp-env-server/README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Environment Variables MCP Server
2+
3+
This sample demonstrates how to create an MCP server that provides tools for accessing environment variables in a secure and controlled manner.
4+
5+
## Overview
6+
7+
The Environment Variables MCP Server provides three main tools:
8+
9+
1. **get_environment_variable** - Retrieve the value of a specific environment variable
10+
2. **list_environment_variables** - List all environment variables, optionally filtered by pattern
11+
3. **check_environment_variable** - Check if an environment variable exists without returning its value
12+
13+
## Features
14+
15+
- **Secure access** to environment variables with proper error handling
16+
- **Pattern-based filtering** when listing environment variables
17+
- **Existence checking** without value exposure
18+
- **Comprehensive error handling** with detailed status messages
19+
- **Structured responses** with consistent JSON format
20+
21+
## Installation
22+
23+
```bash
24+
uv venv -p 3.11 .venv
25+
.venv\Scripts\activate
26+
uv sync
27+
```
28+
29+
## Usage
30+
31+
### Get Environment Variable
32+
33+
Retrieve the value of a specific environment variable:
34+
35+
```python
36+
# Example usage
37+
get_environment_variable("PATH")
38+
# Returns: {"status": "success", "name": "PATH", "value": "/usr/bin:/bin", "message": "Environment variable 'PATH' retrieved successfully"}
39+
40+
get_environment_variable("NON_EXISTENT_VAR")
41+
# Returns: {"status": "not_found", "name": "NON_EXISTENT_VAR", "value": None, "message": "Environment variable 'NON_EXISTENT_VAR' is not set"}
42+
```
43+
44+
### List Environment Variables
45+
46+
List all environment variables or filter by pattern:
47+
48+
```python
49+
# List all environment variables
50+
list_environment_variables()
51+
52+
# Filter by pattern (case-insensitive)
53+
list_environment_variables("PATH")
54+
# Returns variables containing "PATH" in their name
55+
```
56+
57+
### Check Environment Variable
58+
59+
Check if an environment variable exists without exposing its value:
60+
61+
```python
62+
check_environment_variable("HOME")
63+
# Returns: {"status": "success", "name": "HOME", "exists": True, "message": "Environment variable 'HOME' exists"}
64+
```
65+
66+
## Security Considerations
67+
68+
- Environment variables may contain sensitive information (API keys, passwords, etc.)
69+
- Use with caution in production environments
70+
- Consider implementing access controls or filtering for sensitive variables
71+
- The `check_environment_variable` tool provides a way to verify existence without exposing values
72+
73+
## Configuration
74+
75+
The server is configured through standard MCP configuration files:
76+
77+
- `mcp.json` - Defines the server transport and command
78+
- `pyproject.toml` - Python project configuration and dependencies
79+
- `uipath.json` - UiPath platform integration settings
80+
81+
## Running the Server
82+
83+
To run the server locally for debugging:
84+
85+
```bash
86+
uipath run env-server
87+
```
88+
89+
To package and deploy:
90+
91+
```bash
92+
uipath pack
93+
uipath publish
94+
```
95+
96+
## Example Responses
97+
98+
All tools return structured JSON responses with consistent formatting:
99+
100+
```json
101+
{
102+
"status": "success|not_found|error",
103+
"name": "VARIABLE_NAME",
104+
"value": "variable_value_or_null",
105+
"message": "Descriptive message about the operation"
106+
}
107+
```
108+
109+
For listing operations:
110+
111+
```json
112+
{
113+
"status": "success|error",
114+
"count": 42,
115+
"variables": {"VAR1": "value1", "VAR2": "value2"},
116+
"message": "Found 42 environment variables"
117+
}
118+
```

samples/mcp-env-server/mcp.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"servers": {
3+
"env-server": {
4+
"transport": "stdio",
5+
"command": "python",
6+
"args": ["server.py"]
7+
}
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[project]
2+
name = "mcp-env-server"
3+
version = "0.0.1"
4+
description = "Environment Variables MCP Server"
5+
authors = [{ name = "John Doe" }]
6+
dependencies = [
7+
"uipath-mcp>=0.0.78",
8+
]
9+
requires-python = ">=3.10"

samples/mcp-env-server/server.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import os
2+
from typing import Dict, Any, Optional
3+
4+
from mcp.server.fastmcp import FastMCP
5+
6+
mcp = FastMCP("Environment Variables MCP Server")
7+
8+
9+
@mcp.tool()
10+
def get_environment_variable(name: str) -> Dict[str, Any]:
11+
"""Get the value of an environment variable by name.
12+
13+
Args:
14+
name: The name of the environment variable to retrieve
15+
16+
Returns:
17+
Dictionary containing the variable name, value, and status information
18+
"""
19+
try:
20+
value = os.environ.get(name)
21+
22+
if value is None:
23+
return {
24+
"status": "not_found",
25+
"name": name,
26+
"value": None,
27+
"message": f"Environment variable '{name}' is not set"
28+
}
29+
30+
return {
31+
"status": "success",
32+
"name": name,
33+
"value": value,
34+
"message": f"Environment variable '{name}' retrieved successfully"
35+
}
36+
37+
except Exception as e:
38+
return {
39+
"status": "error",
40+
"name": name,
41+
"value": None,
42+
"message": f"Error retrieving environment variable '{name}': {str(e)}"
43+
}
44+
45+
46+
@mcp.tool()
47+
def list_environment_variables(pattern: Optional[str] = None) -> Dict[str, Any]:
48+
"""List all environment variables, optionally filtered by a pattern.
49+
50+
Args:
51+
pattern: Optional pattern to filter environment variable names (case-insensitive substring match)
52+
53+
Returns:
54+
Dictionary containing the list of environment variables and their values
55+
"""
56+
try:
57+
env_vars = dict(os.environ)
58+
59+
if pattern:
60+
# Filter environment variables by pattern (case-insensitive)
61+
filtered_vars = {
62+
key: value for key, value in env_vars.items()
63+
if pattern.lower() in key.lower()
64+
}
65+
else:
66+
filtered_vars = env_vars
67+
68+
return {
69+
"status": "success",
70+
"count": len(filtered_vars),
71+
"variables": filtered_vars,
72+
"message": f"Found {len(filtered_vars)} environment variables" +
73+
(f" matching pattern '{pattern}'" if pattern else "")
74+
}
75+
76+
except Exception as e:
77+
return {
78+
"status": "error",
79+
"count": 0,
80+
"variables": {},
81+
"message": f"Error listing environment variables: {str(e)}"
82+
}
83+
84+
85+
@mcp.tool()
86+
def check_environment_variable(name: str) -> Dict[str, Any]:
87+
"""Check if an environment variable exists without returning its value.
88+
89+
Args:
90+
name: The name of the environment variable to check
91+
92+
Returns:
93+
Dictionary containing the existence status of the environment variable
94+
"""
95+
try:
96+
exists = name in os.environ
97+
98+
return {
99+
"status": "success",
100+
"name": name,
101+
"exists": exists,
102+
"message": f"Environment variable '{name}' {'exists' if exists else 'does not exist'}"
103+
}
104+
105+
except Exception as e:
106+
return {
107+
"status": "error",
108+
"name": name,
109+
"exists": False,
110+
"message": f"Error checking environment variable '{name}': {str(e)}"
111+
}
112+
113+
114+
# Run the server when the script is executed
115+
if __name__ == "__main__":
116+
mcp.run()

samples/mcp-env-server/uipath.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"entryPoints": [
3+
{
4+
"filePath": "env-server",
5+
"uniqueId": "a1b2c3d4-5678-90ab-cdef-123456789012",
6+
"type": "mcpserver",
7+
"input": {},
8+
"output": {}
9+
}
10+
]
11+
}

0 commit comments

Comments
 (0)