mcpshield-agent 0.1.0__tar.gz
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.
- mcpshield_agent-0.1.0/PKG-INFO +149 -0
- mcpshield_agent-0.1.0/README.md +122 -0
- mcpshield_agent-0.1.0/mcpshield_agent/__init__.py +5 -0
- mcpshield_agent-0.1.0/mcpshield_agent/__main__.py +13 -0
- mcpshield_agent-0.1.0/mcpshield_agent/cli.py +327 -0
- mcpshield_agent-0.1.0/mcpshield_agent/client.py +68 -0
- mcpshield_agent-0.1.0/mcpshield_agent/config.py +65 -0
- mcpshield_agent-0.1.0/mcpshield_agent/reporter.py +261 -0
- mcpshield_agent-0.1.0/mcpshield_agent/scanner.py +225 -0
- mcpshield_agent-0.1.0/mcpshield_agent/service.py +439 -0
- mcpshield_agent-0.1.0/mcpshield_agent.egg-info/PKG-INFO +149 -0
- mcpshield_agent-0.1.0/mcpshield_agent.egg-info/SOURCES.txt +18 -0
- mcpshield_agent-0.1.0/mcpshield_agent.egg-info/dependency_links.txt +1 -0
- mcpshield_agent-0.1.0/mcpshield_agent.egg-info/entry_points.txt +2 -0
- mcpshield_agent-0.1.0/mcpshield_agent.egg-info/requires.txt +3 -0
- mcpshield_agent-0.1.0/mcpshield_agent.egg-info/top_level.txt +1 -0
- mcpshield_agent-0.1.0/pyproject.toml +66 -0
- mcpshield_agent-0.1.0/setup.cfg +4 -0
- mcpshield_agent-0.1.0/tests/test_cli.py +90 -0
- mcpshield_agent-0.1.0/tests/test_scanner.py +240 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mcpshield-agent
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: MCPShield Agent - AI Agent Security Scanner for MCP servers
|
|
5
|
+
Author-email: David Cooper <hello@mcpshield.app>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://mcpshield.app
|
|
8
|
+
Project-URL: Repository, https://github.com/RunTimeAdmin/MCPShield
|
|
9
|
+
Project-URL: Issues, https://github.com/RunTimeAdmin/MCPShield/issues
|
|
10
|
+
Project-URL: Documentation, https://mcpshield.app/docs
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Intended Audience :: System Administrators
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Topic :: Security
|
|
21
|
+
Classifier: Topic :: System :: Systems Administration
|
|
22
|
+
Requires-Python: >=3.9
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
Requires-Dist: click>=8.1.0
|
|
25
|
+
Requires-Dist: requests>=2.31.0
|
|
26
|
+
Requires-Dist: psutil>=5.9.0
|
|
27
|
+
|
|
28
|
+
# MCPShield Agent
|
|
29
|
+
|
|
30
|
+
AI Agent Security Scanner - Discovers MCP servers on your system and reports them to MCPShield for security analysis.
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
# From source
|
|
36
|
+
pip install -e .
|
|
37
|
+
|
|
38
|
+
# Or from PyPI (when published)
|
|
39
|
+
pip install mcpshield-agent
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Quick Start
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
# 1. Configure with your API key (get from dashboard)
|
|
46
|
+
mcpshield configure --api-key mcp_sk_your_key_here
|
|
47
|
+
|
|
48
|
+
# 2. Scan for MCP servers
|
|
49
|
+
mcpshield scan
|
|
50
|
+
|
|
51
|
+
# 3. Check status
|
|
52
|
+
mcpshield status
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Commands
|
|
56
|
+
|
|
57
|
+
| Command | Description |
|
|
58
|
+
|---------|-------------|
|
|
59
|
+
| `mcpshield configure --api-key KEY` | Configure agent with API key |
|
|
60
|
+
| `mcpshield scan` | Scan and report MCP servers |
|
|
61
|
+
| `mcpshield scan --dry-run` | Scan without reporting |
|
|
62
|
+
| `mcpshield daemon` | Run continuous scheduled scanning |
|
|
63
|
+
| `mcpshield status` | Show agent status |
|
|
64
|
+
| `mcpshield list` | List found servers (no report) |
|
|
65
|
+
| `mcpshield --version` | Show version |
|
|
66
|
+
|
|
67
|
+
## What It Scans
|
|
68
|
+
|
|
69
|
+
The agent looks for MCP server configurations in:
|
|
70
|
+
|
|
71
|
+
**Windows:**
|
|
72
|
+
- `%APPDATA%\Claude\claude_desktop_config.json`
|
|
73
|
+
- `%APPDATA%\Cursor\User\globalStorage\saoudrizwan.claude-dev\settings\cline_mcp_settings.json`
|
|
74
|
+
|
|
75
|
+
**macOS:**
|
|
76
|
+
- `~/Library/Application Support/Claude/claude_desktop_config.json`
|
|
77
|
+
|
|
78
|
+
**Linux:**
|
|
79
|
+
- `~/.config/Claude/claude_desktop_config.json`
|
|
80
|
+
- `~/.config/cursor/mcp.json`
|
|
81
|
+
|
|
82
|
+
## What It Reports
|
|
83
|
+
|
|
84
|
+
For each discovered MCP server:
|
|
85
|
+
|
|
86
|
+
- **Server name** - e.g., "filesystem", "postgres"
|
|
87
|
+
- **Server type** - e.g., "@modelcontextprotocol/server-filesystem"
|
|
88
|
+
- **Command** - Full command string
|
|
89
|
+
- **Scope** - Access scope (file paths, URLs)
|
|
90
|
+
- **Environment variables** - Names only, NOT values
|
|
91
|
+
- **Status** - Active or dormant
|
|
92
|
+
|
|
93
|
+
## Configuration
|
|
94
|
+
|
|
95
|
+
Config is stored in:
|
|
96
|
+
- Windows: `%LOCALAPPDATA%\MCPShield\config.json`
|
|
97
|
+
- macOS/Linux: `~/.config/mcpshield/config.json`
|
|
98
|
+
|
|
99
|
+
```json
|
|
100
|
+
{
|
|
101
|
+
"api_url": "https://api.mcpshield.app",
|
|
102
|
+
"api_key": "mcp_sk_...",
|
|
103
|
+
"scan_interval": 300
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Daemon Mode (Continuous Scanning)
|
|
108
|
+
|
|
109
|
+
Run the agent in daemon mode for automatic scheduled scanning:
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
# Default: scan every hour (3600 seconds)
|
|
113
|
+
mcpshield daemon
|
|
114
|
+
|
|
115
|
+
# Custom interval: scan every 5 minutes
|
|
116
|
+
mcpshield daemon --interval 300
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
The daemon will:
|
|
120
|
+
- Scan for MCP servers at the configured interval
|
|
121
|
+
- Report discovered servers to the backend
|
|
122
|
+
- Send heartbeat updates
|
|
123
|
+
- Log each scan cycle
|
|
124
|
+
- Shut down gracefully on Ctrl+C
|
|
125
|
+
|
|
126
|
+
Configure the default interval in your config file (`scan_interval` key).
|
|
127
|
+
|
|
128
|
+
## Security
|
|
129
|
+
|
|
130
|
+
- **Never sends credential values** - Only environment variable names
|
|
131
|
+
- **Local config is secure** - API key stored locally
|
|
132
|
+
- **HTTPS by default** - All API communication encrypted
|
|
133
|
+
|
|
134
|
+
## Development
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
# Install in development mode
|
|
138
|
+
pip install -e .
|
|
139
|
+
|
|
140
|
+
# Run tests
|
|
141
|
+
pytest
|
|
142
|
+
|
|
143
|
+
# Run locally against dev API
|
|
144
|
+
mcpshield configure --api-key YOUR_KEY --api-url http://localhost:8000
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## License
|
|
148
|
+
|
|
149
|
+
MIT License - see LICENSE file.
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# MCPShield Agent
|
|
2
|
+
|
|
3
|
+
AI Agent Security Scanner - Discovers MCP servers on your system and reports them to MCPShield for security analysis.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# From source
|
|
9
|
+
pip install -e .
|
|
10
|
+
|
|
11
|
+
# Or from PyPI (when published)
|
|
12
|
+
pip install mcpshield-agent
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
# 1. Configure with your API key (get from dashboard)
|
|
19
|
+
mcpshield configure --api-key mcp_sk_your_key_here
|
|
20
|
+
|
|
21
|
+
# 2. Scan for MCP servers
|
|
22
|
+
mcpshield scan
|
|
23
|
+
|
|
24
|
+
# 3. Check status
|
|
25
|
+
mcpshield status
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Commands
|
|
29
|
+
|
|
30
|
+
| Command | Description |
|
|
31
|
+
|---------|-------------|
|
|
32
|
+
| `mcpshield configure --api-key KEY` | Configure agent with API key |
|
|
33
|
+
| `mcpshield scan` | Scan and report MCP servers |
|
|
34
|
+
| `mcpshield scan --dry-run` | Scan without reporting |
|
|
35
|
+
| `mcpshield daemon` | Run continuous scheduled scanning |
|
|
36
|
+
| `mcpshield status` | Show agent status |
|
|
37
|
+
| `mcpshield list` | List found servers (no report) |
|
|
38
|
+
| `mcpshield --version` | Show version |
|
|
39
|
+
|
|
40
|
+
## What It Scans
|
|
41
|
+
|
|
42
|
+
The agent looks for MCP server configurations in:
|
|
43
|
+
|
|
44
|
+
**Windows:**
|
|
45
|
+
- `%APPDATA%\Claude\claude_desktop_config.json`
|
|
46
|
+
- `%APPDATA%\Cursor\User\globalStorage\saoudrizwan.claude-dev\settings\cline_mcp_settings.json`
|
|
47
|
+
|
|
48
|
+
**macOS:**
|
|
49
|
+
- `~/Library/Application Support/Claude/claude_desktop_config.json`
|
|
50
|
+
|
|
51
|
+
**Linux:**
|
|
52
|
+
- `~/.config/Claude/claude_desktop_config.json`
|
|
53
|
+
- `~/.config/cursor/mcp.json`
|
|
54
|
+
|
|
55
|
+
## What It Reports
|
|
56
|
+
|
|
57
|
+
For each discovered MCP server:
|
|
58
|
+
|
|
59
|
+
- **Server name** - e.g., "filesystem", "postgres"
|
|
60
|
+
- **Server type** - e.g., "@modelcontextprotocol/server-filesystem"
|
|
61
|
+
- **Command** - Full command string
|
|
62
|
+
- **Scope** - Access scope (file paths, URLs)
|
|
63
|
+
- **Environment variables** - Names only, NOT values
|
|
64
|
+
- **Status** - Active or dormant
|
|
65
|
+
|
|
66
|
+
## Configuration
|
|
67
|
+
|
|
68
|
+
Config is stored in:
|
|
69
|
+
- Windows: `%LOCALAPPDATA%\MCPShield\config.json`
|
|
70
|
+
- macOS/Linux: `~/.config/mcpshield/config.json`
|
|
71
|
+
|
|
72
|
+
```json
|
|
73
|
+
{
|
|
74
|
+
"api_url": "https://api.mcpshield.app",
|
|
75
|
+
"api_key": "mcp_sk_...",
|
|
76
|
+
"scan_interval": 300
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Daemon Mode (Continuous Scanning)
|
|
81
|
+
|
|
82
|
+
Run the agent in daemon mode for automatic scheduled scanning:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
# Default: scan every hour (3600 seconds)
|
|
86
|
+
mcpshield daemon
|
|
87
|
+
|
|
88
|
+
# Custom interval: scan every 5 minutes
|
|
89
|
+
mcpshield daemon --interval 300
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
The daemon will:
|
|
93
|
+
- Scan for MCP servers at the configured interval
|
|
94
|
+
- Report discovered servers to the backend
|
|
95
|
+
- Send heartbeat updates
|
|
96
|
+
- Log each scan cycle
|
|
97
|
+
- Shut down gracefully on Ctrl+C
|
|
98
|
+
|
|
99
|
+
Configure the default interval in your config file (`scan_interval` key).
|
|
100
|
+
|
|
101
|
+
## Security
|
|
102
|
+
|
|
103
|
+
- **Never sends credential values** - Only environment variable names
|
|
104
|
+
- **Local config is secure** - API key stored locally
|
|
105
|
+
- **HTTPS by default** - All API communication encrypted
|
|
106
|
+
|
|
107
|
+
## Development
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
# Install in development mode
|
|
111
|
+
pip install -e .
|
|
112
|
+
|
|
113
|
+
# Run tests
|
|
114
|
+
pytest
|
|
115
|
+
|
|
116
|
+
# Run locally against dev API
|
|
117
|
+
mcpshield configure --api-key YOUR_KEY --api-url http://localhost:8000
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## License
|
|
121
|
+
|
|
122
|
+
MIT License - see LICENSE file.
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
"""MCPShield Agent - CLI Interface"""
|
|
2
|
+
|
|
3
|
+
import click
|
|
4
|
+
import os
|
|
5
|
+
import socket
|
|
6
|
+
import platform
|
|
7
|
+
|
|
8
|
+
from .scanner import MCPScanner
|
|
9
|
+
from .client import MCPShieldClient
|
|
10
|
+
from .config import Config
|
|
11
|
+
from . import __version__
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@click.group()
|
|
15
|
+
@click.version_option(version=__version__, prog_name='mcpshield')
|
|
16
|
+
def cli():
|
|
17
|
+
"""MCPShield Agent - AI Agent Security Scanner
|
|
18
|
+
|
|
19
|
+
Discovers MCP servers on your system and reports them to MCPShield
|
|
20
|
+
for security analysis and risk assessment.
|
|
21
|
+
"""
|
|
22
|
+
pass
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@cli.command()
|
|
26
|
+
@click.option('--api-key', required=True, help='Your MCPShield API key from dashboard')
|
|
27
|
+
@click.option('--api-url', default='http://localhost:8000', help='API URL (default: localhost:8000)')
|
|
28
|
+
def configure(api_key: str, api_url: str):
|
|
29
|
+
"""Configure agent with API key"""
|
|
30
|
+
|
|
31
|
+
# Validate API key format
|
|
32
|
+
if not api_key.startswith('mcp_sk_'):
|
|
33
|
+
click.echo("Error: Invalid API key format. Should start with 'mcp_sk_'")
|
|
34
|
+
return
|
|
35
|
+
|
|
36
|
+
# Test connection
|
|
37
|
+
click.echo(f"Testing connection to {api_url}...")
|
|
38
|
+
client = MCPShieldClient(api_url, api_key)
|
|
39
|
+
|
|
40
|
+
if client.test_connection():
|
|
41
|
+
click.echo("Connected successfully!")
|
|
42
|
+
else:
|
|
43
|
+
click.echo(f"Warning: Could not connect to {api_url}")
|
|
44
|
+
click.echo("Saving config anyway - API might be down temporarily")
|
|
45
|
+
|
|
46
|
+
# Save config
|
|
47
|
+
config = Config.load()
|
|
48
|
+
config['api_key'] = api_key
|
|
49
|
+
config['api_url'] = api_url
|
|
50
|
+
Config.save(config)
|
|
51
|
+
|
|
52
|
+
click.echo("")
|
|
53
|
+
click.echo("Agent configured successfully!")
|
|
54
|
+
click.echo(f" API URL: {api_url}")
|
|
55
|
+
click.echo(f" Config: {Config.CONFIG_FILE}")
|
|
56
|
+
click.echo("")
|
|
57
|
+
click.echo("Next steps:")
|
|
58
|
+
click.echo(" mcpshield scan # Scan for MCP servers")
|
|
59
|
+
click.echo(" mcpshield status # Check agent status")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@cli.command()
|
|
63
|
+
@click.option('--dry-run', is_flag=True, help='Scan but do not report to API')
|
|
64
|
+
def scan(dry_run: bool):
|
|
65
|
+
"""Scan for MCP servers and report to API"""
|
|
66
|
+
|
|
67
|
+
# Load config
|
|
68
|
+
config = Config.load()
|
|
69
|
+
api_key = config.get('api_key')
|
|
70
|
+
|
|
71
|
+
if not api_key and not dry_run:
|
|
72
|
+
click.echo("Error: Not configured. Run: mcpshield configure --api-key YOUR_KEY")
|
|
73
|
+
return
|
|
74
|
+
|
|
75
|
+
# Scan for servers
|
|
76
|
+
click.echo("Scanning for MCP servers...")
|
|
77
|
+
scanner = MCPScanner()
|
|
78
|
+
servers = scanner.scan()
|
|
79
|
+
|
|
80
|
+
if not servers:
|
|
81
|
+
click.echo("No MCP servers found.")
|
|
82
|
+
click.echo("")
|
|
83
|
+
click.echo("Searched in:")
|
|
84
|
+
for path in scanner.config_paths:
|
|
85
|
+
exists = "[exists]" if path.exists() else ""
|
|
86
|
+
click.echo(f" {path} {exists}")
|
|
87
|
+
return
|
|
88
|
+
|
|
89
|
+
click.echo(f"Found {len(servers)} MCP server(s):")
|
|
90
|
+
click.echo("")
|
|
91
|
+
|
|
92
|
+
# Display found servers
|
|
93
|
+
for server in servers:
|
|
94
|
+
status_icon = "[active]" if server['status'] == 'active' else "[dormant]"
|
|
95
|
+
click.echo(f" {server['server_name']} {status_icon}")
|
|
96
|
+
click.echo(f" Type: {server['server_type']}")
|
|
97
|
+
if server.get('scope'):
|
|
98
|
+
click.echo(f" Scope: {server['scope']}")
|
|
99
|
+
if server.get('env_vars'):
|
|
100
|
+
click.echo(f" Env: {', '.join(server['env_vars'])}")
|
|
101
|
+
if server.get('source'):
|
|
102
|
+
click.echo(f" Source: {server['source']}")
|
|
103
|
+
click.echo("")
|
|
104
|
+
|
|
105
|
+
if dry_run:
|
|
106
|
+
click.echo("(Dry run - not reporting to API)")
|
|
107
|
+
return
|
|
108
|
+
|
|
109
|
+
# Report to API
|
|
110
|
+
click.echo("Reporting to MCPShield...")
|
|
111
|
+
client = MCPShieldClient(config['api_url'], api_key)
|
|
112
|
+
|
|
113
|
+
try:
|
|
114
|
+
# Send heartbeat first
|
|
115
|
+
client.heartbeat(
|
|
116
|
+
hostname=socket.gethostname(),
|
|
117
|
+
os_type=platform.system().lower(),
|
|
118
|
+
username=os.getenv('USERNAME', os.getenv('USER', 'unknown')),
|
|
119
|
+
ip_address=_get_ip_address(),
|
|
120
|
+
agent_version=__version__
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
# Report servers
|
|
124
|
+
result = client.report_servers(servers)
|
|
125
|
+
|
|
126
|
+
click.echo("")
|
|
127
|
+
click.echo("Success!")
|
|
128
|
+
click.echo(f" Created: {result.get('created', 0)}")
|
|
129
|
+
click.echo(f" Updated: {result.get('updated', 0)}")
|
|
130
|
+
click.echo(f" Alerts: {result.get('alerts', 0)}")
|
|
131
|
+
|
|
132
|
+
# Show risk info if available
|
|
133
|
+
if 'servers' in result:
|
|
134
|
+
click.echo("")
|
|
135
|
+
click.echo("Risk Assessment:")
|
|
136
|
+
for s in result['servers']:
|
|
137
|
+
risk = s.get('risk_level', 'unknown')
|
|
138
|
+
score = s.get('risk_score', 0)
|
|
139
|
+
click.echo(f" {s['server_name']}: {risk} (score: {score})")
|
|
140
|
+
|
|
141
|
+
except Exception as e:
|
|
142
|
+
click.echo(f"Error: Failed to report - {e}")
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
@cli.command()
|
|
146
|
+
def status():
|
|
147
|
+
"""Show agent configuration and status"""
|
|
148
|
+
|
|
149
|
+
config = Config.load()
|
|
150
|
+
|
|
151
|
+
click.echo("MCPShield Agent Status")
|
|
152
|
+
click.echo("=" * 50)
|
|
153
|
+
click.echo(f"Version: {__version__}")
|
|
154
|
+
click.echo(f"Config file: {Config.CONFIG_FILE}")
|
|
155
|
+
click.echo(f"API URL: {config.get('api_url', 'Not set')}")
|
|
156
|
+
|
|
157
|
+
api_key = config.get('api_key')
|
|
158
|
+
if api_key:
|
|
159
|
+
masked_key = api_key[:12] + "..." + api_key[-4:] if len(api_key) > 20 else api_key[:8] + "..."
|
|
160
|
+
click.echo(f"API Key: {masked_key}")
|
|
161
|
+
else:
|
|
162
|
+
click.echo("API Key: Not configured")
|
|
163
|
+
|
|
164
|
+
click.echo("")
|
|
165
|
+
|
|
166
|
+
# Test API connection
|
|
167
|
+
if api_key:
|
|
168
|
+
client = MCPShieldClient(config['api_url'], api_key)
|
|
169
|
+
if client.test_connection():
|
|
170
|
+
click.echo("API Status: Connected")
|
|
171
|
+
else:
|
|
172
|
+
click.echo("API Status: Cannot connect")
|
|
173
|
+
else:
|
|
174
|
+
click.echo("API Status: Not configured")
|
|
175
|
+
|
|
176
|
+
# Show config paths
|
|
177
|
+
click.echo("")
|
|
178
|
+
click.echo("MCP Config Locations:")
|
|
179
|
+
scanner = MCPScanner()
|
|
180
|
+
for path in scanner.config_paths:
|
|
181
|
+
if path.exists():
|
|
182
|
+
click.echo(f" [found] {path}")
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
@cli.command()
|
|
186
|
+
def list():
|
|
187
|
+
"""List discovered MCP servers (without reporting)"""
|
|
188
|
+
|
|
189
|
+
click.echo("Scanning for MCP servers...")
|
|
190
|
+
scanner = MCPScanner()
|
|
191
|
+
servers = scanner.scan()
|
|
192
|
+
|
|
193
|
+
if not servers:
|
|
194
|
+
click.echo("No MCP servers found.")
|
|
195
|
+
return
|
|
196
|
+
|
|
197
|
+
click.echo(f"Found {len(servers)} MCP server(s):")
|
|
198
|
+
click.echo("")
|
|
199
|
+
|
|
200
|
+
for i, server in enumerate(servers, 1):
|
|
201
|
+
click.echo(f"{i}. {server['server_name']}")
|
|
202
|
+
click.echo(f" Type: {server['server_type']}")
|
|
203
|
+
click.echo(f" Status: {server['status']}")
|
|
204
|
+
if server.get('scope'):
|
|
205
|
+
click.echo(f" Scope: {server['scope']}")
|
|
206
|
+
if server.get('source'):
|
|
207
|
+
click.echo(f" Source: {server['source']}")
|
|
208
|
+
click.echo("")
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
@cli.command()
|
|
212
|
+
@click.option('--interval', default=None, type=int, help='Scan interval in seconds (default: from config, or 3600)')
|
|
213
|
+
def daemon(interval: int):
|
|
214
|
+
"""Run continuous scanning on a schedule.
|
|
215
|
+
|
|
216
|
+
Automatically scans for MCP servers and reports to the backend
|
|
217
|
+
at regular intervals. Press Ctrl+C to stop.
|
|
218
|
+
"""
|
|
219
|
+
import time
|
|
220
|
+
import signal
|
|
221
|
+
import sys
|
|
222
|
+
|
|
223
|
+
config = Config.load()
|
|
224
|
+
|
|
225
|
+
if not Config.is_configured():
|
|
226
|
+
click.echo("Error: Agent not configured. Run 'mcpshield configure' first.")
|
|
227
|
+
sys.exit(1)
|
|
228
|
+
|
|
229
|
+
scan_interval = interval or config.get('scan_interval', 3600)
|
|
230
|
+
|
|
231
|
+
click.echo(f"Starting MCPShield daemon (scanning every {scan_interval}s)")
|
|
232
|
+
click.echo("Press Ctrl+C to stop.\n")
|
|
233
|
+
|
|
234
|
+
running = True
|
|
235
|
+
|
|
236
|
+
def handle_signal(signum, frame):
|
|
237
|
+
nonlocal running
|
|
238
|
+
click.echo("\nShutting down daemon...")
|
|
239
|
+
running = False
|
|
240
|
+
|
|
241
|
+
signal.signal(signal.SIGINT, handle_signal)
|
|
242
|
+
signal.signal(signal.SIGTERM, handle_signal)
|
|
243
|
+
|
|
244
|
+
client = MCPShieldClient(config['api_url'], config['api_key'])
|
|
245
|
+
scanner = MCPScanner()
|
|
246
|
+
cycle = 0
|
|
247
|
+
|
|
248
|
+
while running:
|
|
249
|
+
cycle += 1
|
|
250
|
+
click.echo(f"[Cycle {cycle}] Scanning for MCP servers...")
|
|
251
|
+
|
|
252
|
+
try:
|
|
253
|
+
servers = scanner.scan()
|
|
254
|
+
click.echo(f"[Cycle {cycle}] Found {len(servers)} MCP server(s)")
|
|
255
|
+
|
|
256
|
+
if servers:
|
|
257
|
+
# Send heartbeat
|
|
258
|
+
import platform
|
|
259
|
+
import socket
|
|
260
|
+
hostname = platform.node()
|
|
261
|
+
os_type = platform.system().lower()
|
|
262
|
+
username = os.getenv('USER') or os.getenv('USERNAME') or 'unknown'
|
|
263
|
+
|
|
264
|
+
# Get IP with fallback
|
|
265
|
+
ip = "127.0.0.1"
|
|
266
|
+
try:
|
|
267
|
+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
268
|
+
for target in [("8.8.8.8", 80), ("1.1.1.1", 80)]:
|
|
269
|
+
try:
|
|
270
|
+
s.connect(target)
|
|
271
|
+
ip = s.getsockname()[0]
|
|
272
|
+
break
|
|
273
|
+
except Exception:
|
|
274
|
+
continue
|
|
275
|
+
s.close()
|
|
276
|
+
except Exception:
|
|
277
|
+
pass
|
|
278
|
+
|
|
279
|
+
client.heartbeat(hostname, os_type, username, ip, __version__)
|
|
280
|
+
result = client.report_servers(servers)
|
|
281
|
+
|
|
282
|
+
if result:
|
|
283
|
+
click.echo(f"[Cycle {cycle}] Reported to backend successfully")
|
|
284
|
+
else:
|
|
285
|
+
click.echo(f"[Cycle {cycle}] Warning: Failed to report to backend")
|
|
286
|
+
else:
|
|
287
|
+
click.echo(f"[Cycle {cycle}] No servers found")
|
|
288
|
+
|
|
289
|
+
except Exception as e:
|
|
290
|
+
click.echo(f"[Cycle {cycle}] Error during scan: {e}")
|
|
291
|
+
|
|
292
|
+
click.echo(f"[Cycle {cycle}] Next scan in {scan_interval}s\n")
|
|
293
|
+
|
|
294
|
+
# Sleep in small increments to allow clean shutdown
|
|
295
|
+
elapsed = 0
|
|
296
|
+
while running and elapsed < scan_interval:
|
|
297
|
+
time.sleep(min(1, scan_interval - elapsed))
|
|
298
|
+
elapsed += 1
|
|
299
|
+
|
|
300
|
+
click.echo("Daemon stopped.")
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def _get_ip_address() -> str:
|
|
304
|
+
"""Get local IP address"""
|
|
305
|
+
ip = "127.0.0.1"
|
|
306
|
+
try:
|
|
307
|
+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
308
|
+
for target in [("8.8.8.8", 80), ("1.1.1.1", 80)]:
|
|
309
|
+
try:
|
|
310
|
+
s.connect(target)
|
|
311
|
+
ip = s.getsockname()[0]
|
|
312
|
+
break
|
|
313
|
+
except Exception:
|
|
314
|
+
continue
|
|
315
|
+
s.close()
|
|
316
|
+
except Exception:
|
|
317
|
+
pass
|
|
318
|
+
return ip
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
def main():
|
|
322
|
+
"""Entry point for CLI"""
|
|
323
|
+
cli()
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
if __name__ == '__main__':
|
|
327
|
+
main()
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""MCPShield Agent - API Client"""
|
|
2
|
+
|
|
3
|
+
import requests
|
|
4
|
+
from typing import Dict, List, Optional
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class MCPShieldClient:
|
|
8
|
+
"""HTTP client for MCPShield API"""
|
|
9
|
+
|
|
10
|
+
def __init__(self, api_url: str = "http://localhost:8000", api_key: Optional[str] = None):
|
|
11
|
+
self.api_url = api_url.rstrip('/')
|
|
12
|
+
self.api_key = api_key
|
|
13
|
+
self.session = requests.Session()
|
|
14
|
+
self.session.headers['Content-Type'] = 'application/json'
|
|
15
|
+
|
|
16
|
+
if api_key:
|
|
17
|
+
self.session.headers['X-API-Key'] = api_key
|
|
18
|
+
|
|
19
|
+
def test_connection(self) -> bool:
|
|
20
|
+
"""Test if API is reachable"""
|
|
21
|
+
try:
|
|
22
|
+
response = self.session.get(f"{self.api_url}/health", timeout=5)
|
|
23
|
+
return response.status_code == 200
|
|
24
|
+
except Exception:
|
|
25
|
+
return False
|
|
26
|
+
|
|
27
|
+
def heartbeat(self, hostname: str, os_type: str, username: str,
|
|
28
|
+
ip_address: str, agent_version: str) -> Dict:
|
|
29
|
+
"""
|
|
30
|
+
Send heartbeat to update agent status.
|
|
31
|
+
|
|
32
|
+
POST /api/v1/agents/heartbeat
|
|
33
|
+
"""
|
|
34
|
+
url = f"{self.api_url}/api/v1/agents/heartbeat"
|
|
35
|
+
|
|
36
|
+
payload = {
|
|
37
|
+
"hostname": hostname,
|
|
38
|
+
"os_type": os_type,
|
|
39
|
+
"username": username,
|
|
40
|
+
"ip_address": ip_address,
|
|
41
|
+
"agent_version": agent_version
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
response = self.session.post(url, json=payload, timeout=30)
|
|
45
|
+
response.raise_for_status()
|
|
46
|
+
return response.json()
|
|
47
|
+
|
|
48
|
+
def report_servers(self, servers: List[Dict]) -> Dict:
|
|
49
|
+
"""
|
|
50
|
+
Report discovered MCP servers.
|
|
51
|
+
|
|
52
|
+
POST /api/v1/mcp/report
|
|
53
|
+
"""
|
|
54
|
+
url = f"{self.api_url}/api/v1/mcp/report"
|
|
55
|
+
|
|
56
|
+
payload = {"servers": servers}
|
|
57
|
+
|
|
58
|
+
response = self.session.post(url, json=payload, timeout=30)
|
|
59
|
+
response.raise_for_status()
|
|
60
|
+
return response.json()
|
|
61
|
+
|
|
62
|
+
def get_api_status(self) -> Dict:
|
|
63
|
+
"""Get API health status"""
|
|
64
|
+
try:
|
|
65
|
+
response = self.session.get(f"{self.api_url}/health", timeout=5)
|
|
66
|
+
return response.json()
|
|
67
|
+
except Exception as e:
|
|
68
|
+
return {"status": "error", "message": str(e)}
|