talkops-prometheus-mcp-server 0.1.0__py3-none-any.whl

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 (79) hide show
  1. prometheus_mcp_server/__init__.py +3 -0
  2. prometheus_mcp_server/config.py +475 -0
  3. prometheus_mcp_server/exceptions/__init__.py +19 -0
  4. prometheus_mcp_server/exceptions/custom.py +60 -0
  5. prometheus_mcp_server/main.py +37 -0
  6. prometheus_mcp_server/models/__init__.py +79 -0
  7. prometheus_mcp_server/models/backend.py +36 -0
  8. prometheus_mcp_server/models/common.py +9 -0
  9. prometheus_mcp_server/models/exporter.py +84 -0
  10. prometheus_mcp_server/models/metadata.py +64 -0
  11. prometheus_mcp_server/models/onboarding.py +34 -0
  12. prometheus_mcp_server/models/query.py +64 -0
  13. prometheus_mcp_server/models/rules.py +90 -0
  14. prometheus_mcp_server/models/target.py +67 -0
  15. prometheus_mcp_server/prompts/__init__.py +20 -0
  16. prometheus_mcp_server/prompts/base.py +19 -0
  17. prometheus_mcp_server/prompts/onboarding_prompts.py +247 -0
  18. prometheus_mcp_server/prompts/query_prompts.py +92 -0
  19. prometheus_mcp_server/prompts/registry.py +18 -0
  20. prometheus_mcp_server/prompts/rule_prompts.py +72 -0
  21. prometheus_mcp_server/prompts/troubleshooting_prompts.py +88 -0
  22. prometheus_mcp_server/resources/__init__.py +31 -0
  23. prometheus_mcp_server/resources/backend_resources.py +42 -0
  24. prometheus_mcp_server/resources/base.py +19 -0
  25. prometheus_mcp_server/resources/config_resources.py +31 -0
  26. prometheus_mcp_server/resources/exporter_resources.py +38 -0
  27. prometheus_mcp_server/resources/kubernetes_resources.py +96 -0
  28. prometheus_mcp_server/resources/metadata_resources.py +31 -0
  29. prometheus_mcp_server/resources/registry.py +19 -0
  30. prometheus_mcp_server/resources/rules_resources.py +79 -0
  31. prometheus_mcp_server/resources/static_resources.py +37 -0
  32. prometheus_mcp_server/resources/topology_resources.py +71 -0
  33. prometheus_mcp_server/resources/tsdb_resources.py +31 -0
  34. prometheus_mcp_server/server/__init__.py +9 -0
  35. prometheus_mcp_server/server/bootstrap.py +47 -0
  36. prometheus_mcp_server/server/core.py +28 -0
  37. prometheus_mcp_server/server/middleware.py +106 -0
  38. prometheus_mcp_server/services/__init__.py +9 -0
  39. prometheus_mcp_server/services/kubernetes_service.py +377 -0
  40. prometheus_mcp_server/services/prometheus_service.py +697 -0
  41. prometheus_mcp_server/static/PROMETHEUS_BEST_PRACTICES.md +38 -0
  42. prometheus_mcp_server/static/PROMETHEUS_MCP_INSTRUCTIONS.md +103 -0
  43. prometheus_mcp_server/static/PROMETHEUS_ONBOARDING_GUIDE.md +50 -0
  44. prometheus_mcp_server/static/__init__.py +1 -0
  45. prometheus_mcp_server/tools/__init__.py +50 -0
  46. prometheus_mcp_server/tools/authoring/__init__.py +1 -0
  47. prometheus_mcp_server/tools/authoring/authoring_tools.py +477 -0
  48. prometheus_mcp_server/tools/base.py +39 -0
  49. prometheus_mcp_server/tools/diagnostics/__init__.py +1 -0
  50. prometheus_mcp_server/tools/diagnostics/diagnostics_tools.py +8 -0
  51. prometheus_mcp_server/tools/discovery/__init__.py +1 -0
  52. prometheus_mcp_server/tools/discovery/discovery_tools.py +8 -0
  53. prometheus_mcp_server/tools/exporter/__init__.py +1 -0
  54. prometheus_mcp_server/tools/exporter/exporter_tools.py +302 -0
  55. prometheus_mcp_server/tools/onboarding/__init__.py +1 -0
  56. prometheus_mcp_server/tools/onboarding/onboarding_tools.py +186 -0
  57. prometheus_mcp_server/tools/promtool/__init__.py +1 -0
  58. prometheus_mcp_server/tools/promtool/promtool_tools.py +107 -0
  59. prometheus_mcp_server/tools/query/__init__.py +1 -0
  60. prometheus_mcp_server/tools/query/query_tools.py +201 -0
  61. prometheus_mcp_server/tools/registry.py +22 -0
  62. prometheus_mcp_server/tools/rules/__init__.py +1 -0
  63. prometheus_mcp_server/tools/rules/rules_tools.py +380 -0
  64. prometheus_mcp_server/tools/scrape_config/__init__.py +1 -0
  65. prometheus_mcp_server/tools/scrape_config/scrape_config_tools.py +315 -0
  66. prometheus_mcp_server/tools/simulation/__init__.py +1 -0
  67. prometheus_mcp_server/tools/simulation/simulation_tools.py +348 -0
  68. prometheus_mcp_server/tools/tsdb_finops/__init__.py +1 -0
  69. prometheus_mcp_server/tools/tsdb_finops/tsdb_finops_tools.py +309 -0
  70. prometheus_mcp_server/utils/__init__.py +25 -0
  71. prometheus_mcp_server/utils/endpoint_tester.py +56 -0
  72. prometheus_mcp_server/utils/exporter_catalog.py +262 -0
  73. prometheus_mcp_server/utils/json_coerce.py +60 -0
  74. prometheus_mcp_server/utils/promql_helpers.py +104 -0
  75. prometheus_mcp_server/utils/promtool_runner.py +211 -0
  76. talkops_prometheus_mcp_server-0.1.0.dist-info/METADATA +605 -0
  77. talkops_prometheus_mcp_server-0.1.0.dist-info/RECORD +79 -0
  78. talkops_prometheus_mcp_server-0.1.0.dist-info/WHEEL +4 -0
  79. talkops_prometheus_mcp_server-0.1.0.dist-info/entry_points.txt +2 -0
@@ -0,0 +1,3 @@
1
+ """Prometheus MCP Server package."""
2
+
3
+ __version__ = '0.1.0'
@@ -0,0 +1,475 @@
1
+ """Application configuration management."""
2
+
3
+ import json
4
+ import os
5
+ from dataclasses import dataclass, field
6
+ from typing import Dict, List, Literal, Optional
7
+
8
+ from prometheus_mcp_server.models.exporter import (
9
+ ExporterConfigField,
10
+ ExporterConfigModel,
11
+ ExporterInfo,
12
+ K8sNuances,
13
+ )
14
+
15
+ # Registry of supported exporters with default configurations
16
+ SUPPORTED_EXPORTERS: Dict[str, ExporterInfo] = {
17
+ # 1. node_exporter
18
+ "node_exporter": ExporterInfo(
19
+ type="node_exporter",
20
+ description="Hardware and OS metrics for Linux nodes.",
21
+ supported_environments=["kubernetes", "vm"],
22
+ default_scope="daemonset",
23
+ default_ports={"metrics": 9100},
24
+ image="prom/node-exporter:latest",
25
+ k8s_nuances=K8sNuances(
26
+ requires_rbac=False,
27
+ supports_sidecar=False,
28
+ # Ideally needs hostPath mounts for /proc, /sys
29
+ ),
30
+ ),
31
+ # 2. windows_exporter
32
+ "windows_exporter": ExporterInfo(
33
+ type="windows_exporter",
34
+ description="Metrics for Windows servers and services.",
35
+ supported_environments=["vm"],
36
+ default_scope="daemonset",
37
+ default_ports={"metrics": 9182},
38
+ image="prometheuscommunity/windows-exporter:latest",
39
+ ),
40
+ # 3. kube-state-metrics
41
+ "kube-state-metrics": ExporterInfo(
42
+ type="kube-state-metrics",
43
+ description="Translates Kubernetes object state into metrics via K8s API.",
44
+ supported_environments=["kubernetes"],
45
+ default_scope="deployment",
46
+ default_ports={"metrics": 8080},
47
+ image="registry.k8s.io/kube-state-metrics/kube-state-metrics:latest",
48
+ k8s_nuances=K8sNuances(requires_rbac=True),
49
+ ),
50
+ # 4. elasticsearch_exporter
51
+ "elasticsearch_exporter": ExporterInfo(
52
+ type="elasticsearch_exporter",
53
+ description="Elasticsearch cluster and node metrics.",
54
+ supported_environments=["kubernetes", "vm"],
55
+ default_scope="deployment",
56
+ default_ports={"metrics": 9114},
57
+ image="quay.io/prometheuscommunity/elasticsearch-exporter:latest",
58
+ config_model=ExporterConfigModel(
59
+ required=[
60
+ ExporterConfigField(name="ES_URI", type="string", description="Base URI of ES node/cluster", example="http://elasticsearch:9200", maps_to_flag="--es.uri")
61
+ ],
62
+ optional=[
63
+ ExporterConfigField(name="ES_ALL", type="bool", description="Fetch all nodes", example="true", maps_to_flag="--es.all"),
64
+ ExporterConfigField(name="ES_INDICES", type="bool", description="Enable index-level metrics", example="true", maps_to_flag="--es.indices"),
65
+ ExporterConfigField(name="ES_TIMEOUT", type="string", description="HTTP timeout", example="20s", maps_to_flag="--es.timeout")
66
+ ]
67
+ )
68
+ ),
69
+ # 5. kafka_exporter
70
+ "kafka_exporter": ExporterInfo(
71
+ type="kafka_exporter",
72
+ description="Kafka broker & consumer group lag metrics.",
73
+ supported_environments=["kubernetes", "vm"],
74
+ default_scope="deployment",
75
+ default_ports={"metrics": 9308},
76
+ image="danielqsj/kafka-exporter:latest",
77
+ config_model=ExporterConfigModel(
78
+ required=[
79
+ ExporterConfigField(name="KAFKA_BROKERS", type="string", description="Comma-separated brokers", example="kafka-1:9092,kafka-2:9092", maps_to_flag="--kafka.server")
80
+ ],
81
+ optional=[
82
+ ExporterConfigField(name="KAFKA_SASL_ENABLED", type="bool", description="Enables SASL flags", example="true"),
83
+ ExporterConfigField(name="KAFKA_SASL_USER", type="string", description="SASL username", example="monitoring-user"),
84
+ ExporterConfigField(name="KAFKA_SASL_PASSWORD", type="string", description="SASL password", example="secure-pass"),
85
+ ExporterConfigField(name="KAFKA_TLS_ENABLED", type="bool", description="TLS client mode", example="true")
86
+ ]
87
+ )
88
+ ),
89
+ # 6. rabbitmq_exporter
90
+ "rabbitmq_exporter": ExporterInfo(
91
+ type="rabbitmq_exporter",
92
+ description="RabbitMQ queue & node metrics via HTTP API.",
93
+ supported_environments=["kubernetes", "vm"],
94
+ default_scope="deployment",
95
+ default_ports={"metrics": 9419},
96
+ image="kbudde/rabbitmq-exporter:latest",
97
+ config_model=ExporterConfigModel(
98
+ required=[
99
+ ExporterConfigField(name="RABBIT_URL", type="string", description="Management HTTP endpoint", example="http://rabbitmq:15672"),
100
+ ExporterConfigField(name="RABBIT_USER", type="string", description="Management user", example="exporter"),
101
+ ExporterConfigField(name="RABBIT_PASSWORD", type="string", description="User password", example="s3cr3t")
102
+ ]
103
+ )
104
+ ),
105
+ # 7. redis_exporter
106
+ "redis_exporter": ExporterInfo(
107
+ type="redis_exporter",
108
+ description="Redis instance and cluster metrics.",
109
+ supported_environments=["kubernetes", "vm"],
110
+ default_scope="sidecar",
111
+ default_ports={"metrics": 9121},
112
+ image="oliver006/redis_exporter:latest",
113
+ config_model=ExporterConfigModel(
114
+ required=[
115
+ ExporterConfigField(name="REDIS_ADDR", type="string", description="Redis connection string", example="redis://redis:6379", maps_to_flag="--redis.addr")
116
+ ],
117
+ optional=[
118
+ ExporterConfigField(name="REDIS_PASSWORD", type="string", description="For AUTH-enabled Redis", example="password", maps_to_flag="--redis.password"),
119
+ ExporterConfigField(name="REDIS_ALIAS", type="string", description="Prefix / label for metrics", example="cart-cache")
120
+ ]
121
+ ),
122
+ k8s_nuances=K8sNuances(supports_sidecar=True)
123
+ ),
124
+ # 8. memcached_exporter
125
+ "memcached_exporter": ExporterInfo(
126
+ type="memcached_exporter",
127
+ description="Memcached server statistics.",
128
+ supported_environments=["kubernetes", "vm"],
129
+ default_scope="deployment",
130
+ default_ports={"metrics": 9150},
131
+ image="prom/memcached-exporter:latest",
132
+ config_model=ExporterConfigModel(
133
+ required=[
134
+ ExporterConfigField(name="MEMCACHED_ADDRESS", type="string", description="Memcached address", example="memcached:11211", maps_to_flag="--memcached.address")
135
+ ],
136
+ optional=[
137
+ ExporterConfigField(name="MEMCACHED_TLS_CA", type="string", description="CA for TLS connections", example="/etc/tls/ca.pem")
138
+ ]
139
+ )
140
+ ),
141
+ # 9. apache_exporter
142
+ "apache_exporter": ExporterInfo(
143
+ type="apache_exporter",
144
+ description="Apache HTTPD mod_status metrics.",
145
+ supported_environments=["kubernetes", "vm"],
146
+ default_scope="sidecar",
147
+ default_ports={"metrics": 9117},
148
+ image="quay.io/prometheuscommunity/apache-exporter:latest",
149
+ config_model=ExporterConfigModel(
150
+ required=[
151
+ ExporterConfigField(name="APACHE_SCRAPE_URI", type="string", description="Status endpoint", example="http://localhost/server-status?auto", maps_to_flag="--scrape_uri")
152
+ ]
153
+ ),
154
+ k8s_nuances=K8sNuances(supports_sidecar=True)
155
+ ),
156
+ # 10. php_fpm_exporter
157
+ "php_fpm_exporter": ExporterInfo(
158
+ type="php_fpm_exporter",
159
+ description="PHP-FPM pool metrics (processes, queue).",
160
+ supported_environments=["kubernetes", "vm"],
161
+ default_scope="sidecar",
162
+ default_ports={"metrics": 9253},
163
+ image="hipages/php-fpm_exporter:latest",
164
+ config_model=ExporterConfigModel(
165
+ required=[
166
+ ExporterConfigField(name="PHP_FPM_SCRAPE_URI", type="string", description="FastCGI endpoint", example="tcp://127.0.0.1:9000/status")
167
+ ]
168
+ ),
169
+ k8s_nuances=K8sNuances(supports_sidecar=True)
170
+ ),
171
+ # 11. statsd_exporter
172
+ "statsd_exporter": ExporterInfo(
173
+ type="statsd_exporter",
174
+ description="StatsD protocol to Prometheus bridge.",
175
+ supported_environments=["kubernetes", "vm"],
176
+ default_scope="deployment",
177
+ default_ports={"metrics": 9102, "statsd": 9125},
178
+ image="prom/statsd-exporter:latest",
179
+ config_model=ExporterConfigModel(
180
+ optional=[
181
+ ExporterConfigField(name="STATSD_EXPORTER_MAPPING_CONFIG", type="string", description="YAML mapping config", example="/etc/statsd-exporter/mapping.yml")
182
+ ]
183
+ ),
184
+ k8s_nuances=K8sNuances(requires_udp_service=True, requires_configmap=True, configmap_mount_path="/etc/statsd-exporter")
185
+ ),
186
+ # 12. jmx_exporter
187
+ "jmx_exporter": ExporterInfo(
188
+ type="jmx_exporter",
189
+ description="JMX to Prometheus for JVM apps.",
190
+ supported_environments=["kubernetes", "vm"],
191
+ default_scope="sidecar",
192
+ default_ports={"metrics": 5556},
193
+ image="solsson/jmx-prometheus-exporter:latest", # common sidecar image
194
+ config_model=ExporterConfigModel(
195
+ required=[
196
+ ExporterConfigField(name="JMX_CONFIG_PATH", type="string", description="MBean to metric mapping", example="/etc/jmx/config.yml")
197
+ ]
198
+ ),
199
+ k8s_nuances=K8sNuances(requires_configmap=True, configmap_mount_path="/etc/jmx", supports_sidecar=True)
200
+ ),
201
+ # 13. aws_cloudwatch_exporter (YACE)
202
+ "aws_cloudwatch_exporter": ExporterInfo(
203
+ type="aws_cloudwatch_exporter",
204
+ description="High-performance AWS CloudWatch metrics exporter (YACE).",
205
+ supported_environments=["kubernetes", "vm"],
206
+ default_scope="deployment",
207
+ default_ports={"metrics": 5000},
208
+ image="quay.io/prometheuscommunity/yet-another-cloudwatch-exporter:latest",
209
+ config_model=ExporterConfigModel(
210
+ required=[
211
+ ExporterConfigField(name="CLOUDWATCH_CONFIG_PATH", type="string", description="YACE Config file", example="/tmp/config.yml", maps_to_flag="--config.file"),
212
+ ExporterConfigField(name="AWS_REGION", type="string", description="Default region", example="eu-west-1")
213
+ ],
214
+ optional=[
215
+ ExporterConfigField(name="AWS_ROLE_ARN", type="string", description="IAM role for cross-account access", example="arn:aws:iam::123:role/yace")
216
+ ]
217
+ ),
218
+ k8s_nuances=K8sNuances(requires_configmap=True, configmap_mount_path="/tmp")
219
+ ),
220
+ # 14. postgres_exporter
221
+ "postgres_exporter": ExporterInfo(
222
+ type="postgres_exporter",
223
+ description="PostgreSQL server metrics (connections, locks, replication).",
224
+ supported_environments=["kubernetes", "vm"],
225
+ default_scope="sidecar",
226
+ default_ports={"metrics": 9187},
227
+ image="quay.io/prometheuscommunity/postgres-exporter:latest",
228
+ config_model=ExporterConfigModel(
229
+ required=[
230
+ ExporterConfigField(name="DATA_SOURCE_URI", type="string", description="Host, port, database, SSL mode", example="localhost:5432/postgres?sslmode=disable"),
231
+ ExporterConfigField(name="DATA_SOURCE_USER", type="string", description="DB user for monitoring", example="postgres"),
232
+ ExporterConfigField(name="DATA_SOURCE_PASS", type="string", description="DB password", example="password")
233
+ ]
234
+ ),
235
+ k8s_nuances=K8sNuances(supports_sidecar=True)
236
+ ),
237
+ # 15. mysqld_exporter
238
+ "mysqld_exporter": ExporterInfo(
239
+ type="mysqld_exporter",
240
+ description="MySQL server metrics.",
241
+ supported_environments=["kubernetes", "vm"],
242
+ default_scope="sidecar",
243
+ default_ports={"metrics": 9104},
244
+ image="prom/mysqld-exporter:latest",
245
+ config_model=ExporterConfigModel(
246
+ required=[
247
+ ExporterConfigField(name="DATA_SOURCE_NAME", type="string", description="Standard DSN", example="user:password@(mysql:3306)/")
248
+ ],
249
+ optional=[
250
+ ExporterConfigField(name="MYSQLD_ADDRESS", type="string", description="MySQL address", example="mysql:3306", maps_to_flag="--mysqld.address"),
251
+ ExporterConfigField(name="MYSQLD_CONFIG_FILE", type="string", description="MySQL client config", example="/config.my-cnf", maps_to_flag="--config.my-cnf")
252
+ ]
253
+ ),
254
+ k8s_nuances=K8sNuances(supports_sidecar=True)
255
+ ),
256
+ # 16. mongodb_exporter
257
+ "mongodb_exporter": ExporterInfo(
258
+ type="mongodb_exporter",
259
+ description="MongoDB instance/cluster metrics.",
260
+ supported_environments=["kubernetes", "vm"],
261
+ default_scope="deployment",
262
+ default_ports={"metrics": 9216},
263
+ image="percona/mongodb_exporter:latest",
264
+ config_model=ExporterConfigModel(
265
+ required=[
266
+ ExporterConfigField(name="MONGODB_URI", type="string", description="MongoDB URI", example="mongodb://user:pass@mongodb:27017/admin", maps_to_flag="--mongodb.uri")
267
+ ],
268
+ optional=[
269
+ ExporterConfigField(name="MONGODB_USER", type="string", description="Optional explicit user", example="monitor"),
270
+ ExporterConfigField(name="MONGODB_PASSWORD", type="string", description="Optional explicit password", example="secret"),
271
+ ExporterConfigField(name="MONGODB_EXPORTER_COLLECT_ALL", type="bool", description="Collect all metrics", example="true", maps_to_flag="--collect-all")
272
+ ]
273
+ ),
274
+ k8s_nuances=K8sNuances(supports_sidecar=True)
275
+ ),
276
+ # 17. nginx_exporter
277
+ "nginx_exporter": ExporterInfo(
278
+ type="nginx_exporter",
279
+ description="NGINX / NGINX Plus metrics.",
280
+ supported_environments=["kubernetes", "vm"],
281
+ default_scope="sidecar",
282
+ default_ports={"metrics": 9113},
283
+ image="nginx/nginx-prometheus-exporter:latest",
284
+ config_model=ExporterConfigModel(
285
+ required=[
286
+ ExporterConfigField(name="NGINX_SCRAPE_URI", type="string", description="NGINX stub_status URI", example="http://nginx:8080/stub_status", maps_to_flag="-nginx.scrape-uri")
287
+ ],
288
+ optional=[
289
+ ExporterConfigField(name="NGINX_PLUS", type="bool", description="Use NGINX Plus API", example="true", maps_to_flag="-nginx.plus")
290
+ ]
291
+ ),
292
+ k8s_nuances=K8sNuances(supports_sidecar=True)
293
+ ),
294
+ # 18. blackbox_exporter
295
+ "blackbox_exporter": ExporterInfo(
296
+ type="blackbox_exporter",
297
+ description="Probes HTTP/TCP/ICMP/DNS endpoints remotely.",
298
+ supported_environments=["kubernetes", "vm"],
299
+ default_scope="deployment",
300
+ default_ports={"metrics": 9115},
301
+ image="quay.io/prometheus/blackbox-exporter:latest",
302
+ config_model=ExporterConfigModel(
303
+ required=[
304
+ ExporterConfigField(name="BLACKBOX_CONFIG_PATH", type="string", description="Blackbox config file path", example="/config/blackbox.yml", maps_to_flag="--config.file")
305
+ ]
306
+ ),
307
+ k8s_nuances=K8sNuances(requires_configmap=True, configmap_mount_path="/config"),
308
+ default_config_data='''modules:
309
+ http_2xx:
310
+ prober: http
311
+ timeout: 15s
312
+ http:
313
+ valid_http_versions: ["HTTP/1.1", "HTTP/2.0"]
314
+ valid_status_codes: []
315
+ method: GET
316
+ fail_if_ssl: false
317
+ fail_if_not_ssl: false
318
+ http_post_2xx:
319
+ prober: http
320
+ timeout: 15s
321
+ http:
322
+ valid_http_versions: ["HTTP/1.1", "HTTP/2.0"]
323
+ method: POST
324
+ tcp_connect:
325
+ prober: tcp
326
+ timeout: 15s
327
+ icmp:
328
+ prober: icmp
329
+ timeout: 15s
330
+ dns:
331
+ prober: dns
332
+ timeout: 15s
333
+ dns:
334
+ transport_protocol: "udp"
335
+ '''
336
+ ),
337
+ # 19. snmp_exporter
338
+ "snmp_exporter": ExporterInfo(
339
+ type="snmp_exporter",
340
+ description="SNMP to Prometheus bridge.",
341
+ supported_environments=["kubernetes", "vm"],
342
+ default_scope="deployment",
343
+ default_ports={"metrics": 9116},
344
+ image="quay.io/prometheus/snmp-exporter:latest",
345
+ config_model=ExporterConfigModel(
346
+ required=[
347
+ ExporterConfigField(name="SNMP_CONFIG_PATH", type="string", description="SNMP config file path", example="/etc/prometheus/snmp/snmp.yml", maps_to_flag="--config.file")
348
+ ],
349
+ optional=[
350
+ ExporterConfigField(name="SNMP_WEB_LISTEN_ADDRESS", type="string", description="Web listen address", example="0.0.0.0:9116", maps_to_flag="--web.listen-address")
351
+ ]
352
+ ),
353
+ k8s_nuances=K8sNuances(requires_configmap=True, configmap_mount_path="/etc/prometheus/snmp")
354
+ ),
355
+ }
356
+
357
+
358
+ @dataclass
359
+ class BackendConfig:
360
+ """Configuration for a single Prometheus-compatible backend."""
361
+ id: str = "default"
362
+ base_url: str = "http://localhost:9090"
363
+ type: Literal["prometheus", "thanos", "mimir", "cortex", "victoriametrics", "other"] = "prometheus"
364
+ display_name: Optional[str] = None
365
+ labels: Dict[str, str] = field(default_factory=dict)
366
+ auth_header: Optional[str] = None
367
+ verify_ssl: bool = True
368
+ timeout: int = 30
369
+
370
+
371
+ @dataclass
372
+ class KubernetesConfig:
373
+ """Kubernetes cluster configuration."""
374
+ context_name: Optional[str] = None
375
+ in_cluster: bool = False
376
+ enabled: bool = True
377
+
378
+
379
+ @dataclass
380
+ class LoggingConfig:
381
+ """Logging configuration."""
382
+ level: str = 'INFO'
383
+ format: str = 'json'
384
+ file_path: str = './logs/mcp_server.log'
385
+ max_bytes: int = 10485760 # 10MB
386
+
387
+
388
+ @dataclass
389
+ class ServerConfig:
390
+ """MCP server configuration."""
391
+ name: str = 'prometheus-mcp-server'
392
+ version: str = '0.1.0'
393
+ transport: str = 'stdio'
394
+ host: str = '0.0.0.0'
395
+ port: int = 8767
396
+ path: str = '/mcp'
397
+ # HTTP server timeout settings (in seconds)
398
+ http_timeout: int = 300
399
+ http_keepalive_timeout: int = 5
400
+ http_connect_timeout: int = 60
401
+
402
+ backends: List[BackendConfig] = field(default_factory=lambda: [BackendConfig()])
403
+ kubernetes: KubernetesConfig = field(default_factory=KubernetesConfig)
404
+ logging: LoggingConfig = field(default_factory=LoggingConfig)
405
+
406
+
407
+ class Config:
408
+ """Configuration loader."""
409
+
410
+ @staticmethod
411
+ def from_env() -> ServerConfig:
412
+ """Load configuration from environment variables.
413
+
414
+ Supports two modes:
415
+ - Single backend: PROMETHEUS_BASE_URL sets a single default backend
416
+ - Multi backend: PROMETHEUS_BACKENDS JSON array defines multiple backends
417
+ """
418
+ from dotenv import load_dotenv
419
+ load_dotenv()
420
+
421
+ # Parse backends
422
+ backends_json = os.getenv('PROMETHEUS_BACKENDS')
423
+ if backends_json:
424
+ try:
425
+ raw_backends = json.loads(backends_json)
426
+ backends = [
427
+ BackendConfig(
428
+ id=b.get('id', f'backend-{i}'),
429
+ base_url=b.get('base_url', 'http://localhost:9090'),
430
+ type=b.get('type', 'prometheus'),
431
+ display_name=b.get('display_name'),
432
+ labels=b.get('labels', {}),
433
+ auth_header=b.get('auth_header'),
434
+ verify_ssl=b.get('verify_ssl', True),
435
+ timeout=b.get('timeout', 30),
436
+ )
437
+ for i, b in enumerate(raw_backends)
438
+ ]
439
+ except (json.JSONDecodeError, TypeError):
440
+ backends = [BackendConfig()]
441
+ else:
442
+ # Single backend mode
443
+ backends = [
444
+ BackendConfig(
445
+ id=os.getenv('PROMETHEUS_BACKEND_ID', 'default'),
446
+ base_url=os.getenv('PROMETHEUS_BASE_URL', 'http://localhost:9090'),
447
+ type=os.getenv('PROMETHEUS_TYPE', 'prometheus'), # type: ignore[arg-type]
448
+ display_name=os.getenv('PROMETHEUS_DISPLAY_NAME'),
449
+ auth_header=os.getenv('PROMETHEUS_AUTH_HEADER'),
450
+ verify_ssl=os.getenv('PROMETHEUS_VERIFY_SSL', 'true').lower() == 'true',
451
+ timeout=int(os.getenv('PROMETHEUS_TIMEOUT', '30')),
452
+ )
453
+ ]
454
+
455
+ return ServerConfig(
456
+ name=os.getenv('MCP_SERVER_NAME', 'prometheus-mcp-server'),
457
+ version=os.getenv('MCP_SERVER_VERSION', '0.1.0'),
458
+ transport=os.getenv('MCP_TRANSPORT', 'stdio'),
459
+ host=os.getenv('MCP_HOST', '0.0.0.0'),
460
+ port=int(os.getenv('MCP_PORT', '8767')),
461
+ path=os.getenv('MCP_PATH', '/mcp'),
462
+ http_timeout=int(os.getenv('MCP_HTTP_TIMEOUT', '300')),
463
+ http_keepalive_timeout=int(os.getenv('MCP_HTTP_KEEPALIVE_TIMEOUT', '5')),
464
+ http_connect_timeout=int(os.getenv('MCP_HTTP_CONNECT_TIMEOUT', '60')),
465
+ backends=backends,
466
+ kubernetes=KubernetesConfig(
467
+ context_name=os.getenv('K8S_CONTEXT'),
468
+ in_cluster=os.getenv('K8S_IN_CLUSTER', 'false').lower() == 'true',
469
+ enabled=os.getenv('K8S_ENABLED', 'true').lower() == 'true',
470
+ ),
471
+ logging=LoggingConfig(
472
+ level=os.getenv('MCP_LOG_LEVEL', 'INFO'),
473
+ format=os.getenv('MCP_LOG_FORMAT', 'json'),
474
+ ),
475
+ )
@@ -0,0 +1,19 @@
1
+ """Exception module."""
2
+
3
+ from prometheus_mcp_server.exceptions.custom import (
4
+ PrometheusConnectionError,
5
+ PrometheusOperationError,
6
+ PrometheusQueryError,
7
+ PrometheusResourceError,
8
+ PrometheusResourceNotFoundError,
9
+ PrometheusValidationError,
10
+ )
11
+
12
+ __all__ = [
13
+ 'PrometheusOperationError',
14
+ 'PrometheusResourceError',
15
+ 'PrometheusResourceNotFoundError',
16
+ 'PrometheusValidationError',
17
+ 'PrometheusConnectionError',
18
+ 'PrometheusQueryError',
19
+ ]
@@ -0,0 +1,60 @@
1
+ """Custom exceptions for Prometheus MCP server.
2
+
3
+ Following FastMCP exception hierarchy from:
4
+ https://gofastmcp.com/python-sdk/fastmcp-exceptions
5
+ """
6
+
7
+ from fastmcp.exceptions import (
8
+ NotFoundError,
9
+ ResourceError,
10
+ ToolError,
11
+ ValidationError,
12
+ )
13
+
14
+
15
+ class PrometheusOperationError(ToolError):
16
+ """Error during Prometheus tool operations.
17
+
18
+ Inherits from ToolError for tool-related Prometheus API failures.
19
+ """
20
+ pass
21
+
22
+
23
+ class PrometheusResourceError(ResourceError):
24
+ """Error during Prometheus resource operations.
25
+
26
+ Inherits from ResourceError for resource-related failures.
27
+ """
28
+ pass
29
+
30
+
31
+ class PrometheusResourceNotFoundError(NotFoundError):
32
+ """Error when a Prometheus resource is not found.
33
+
34
+ Inherits from NotFoundError for 404 scenarios (e.g., unknown backend).
35
+ """
36
+ pass
37
+
38
+
39
+ class PrometheusValidationError(ValidationError):
40
+ """Error validating Prometheus parameters or queries.
41
+
42
+ Inherits from ValidationError for input validation failures.
43
+ """
44
+ pass
45
+
46
+
47
+ class PrometheusConnectionError(ToolError):
48
+ """Error connecting to a Prometheus backend.
49
+
50
+ Raised when a backend is unreachable, times out, or returns unexpected responses.
51
+ """
52
+ pass
53
+
54
+
55
+ class PrometheusQueryError(ToolError):
56
+ """Error during PromQL query execution.
57
+
58
+ Raised for counter rule violations, query timeouts, or invalid PromQL.
59
+ """
60
+ pass
@@ -0,0 +1,37 @@
1
+ """Application entry point."""
2
+
3
+ import sys
4
+ from pathlib import Path
5
+
6
+ # Add project root to Python path for direct execution with uv run
7
+ project_root = Path(__file__).parent.parent
8
+ if str(project_root) not in sys.path:
9
+ sys.path.insert(0, str(project_root))
10
+
11
+ from prometheus_mcp_server.server.bootstrap import ServerBootstrap
12
+
13
+
14
+ def main():
15
+ """Run the MCP server."""
16
+ try:
17
+ mcp, config = ServerBootstrap.initialize()
18
+
19
+ # Run FastMCP server with the configured transport
20
+ if config.transport in ('http', 'sse', 'streamable-http'):
21
+ mcp.run(transport=config.transport, host=config.host, port=config.port, path=config.path)
22
+ else:
23
+ mcp.run()
24
+
25
+ except KeyboardInterrupt:
26
+ pass
27
+ except Exception:
28
+ raise
29
+
30
+
31
+ def cli():
32
+ """CLI entry point for the prometheus-mcp-server command."""
33
+ main()
34
+
35
+
36
+ if __name__ == '__main__':
37
+ cli()
@@ -0,0 +1,79 @@
1
+ """Prometheus data models package."""
2
+
3
+ from prometheus_mcp_server.models.common import BasePrometheusModel
4
+ from prometheus_mcp_server.models.backend import (
5
+ BackendCapabilities,
6
+ BackendInfo,
7
+ BackendsSummary,
8
+ )
9
+ from prometheus_mcp_server.models.query import (
10
+ DownsamplingMetadata,
11
+ InstantQueryResult,
12
+ InstantSample,
13
+ LabelTopologyResult,
14
+ RangeQueryResult,
15
+ RangeSeries,
16
+ ValidatePromQLResult,
17
+ )
18
+ from prometheus_mcp_server.models.target import (
19
+ FailedTarget,
20
+ FailedTargetsSummary,
21
+ ServiceInfo,
22
+ ServiceTopology,
23
+ TargetInfo,
24
+ )
25
+ from prometheus_mcp_server.models.metadata import (
26
+ CardinalityOverview,
27
+ CardinalitySummary,
28
+ MetricCatalog,
29
+ MetricMetadata,
30
+ RuntimeConfig,
31
+ TopCardinalityMetric,
32
+ )
33
+ from prometheus_mcp_server.models.onboarding import (
34
+ InstrumentationSnippet,
35
+ InstrumentationStrategy,
36
+ ScrapeEndpointTestResult,
37
+ )
38
+ from prometheus_mcp_server.models.exporter import (
39
+ ExporterInfo,
40
+ ExporterManifestResult,
41
+ ExporterRecommendation,
42
+ ExporterUninstallResult,
43
+ ExporterVerificationResult,
44
+ )
45
+ from prometheus_mcp_server.models.rules import (
46
+ AlertRule,
47
+ RecordingRule,
48
+ RuleGroup,
49
+ RuleGroupList,
50
+ RuleValidationResult,
51
+ RuleTestResult,
52
+ FiringSimulationResult,
53
+ FiringHistoryAnalysis,
54
+ DraftAlertRule,
55
+ )
56
+
57
+ __all__ = [
58
+ 'BasePrometheusModel',
59
+ # Backend
60
+ 'BackendCapabilities', 'BackendInfo', 'BackendsSummary',
61
+ # Query
62
+ 'DownsamplingMetadata', 'InstantQueryResult', 'InstantSample',
63
+ 'LabelTopologyResult', 'RangeQueryResult', 'RangeSeries', 'ValidatePromQLResult',
64
+ # Target
65
+ 'FailedTarget', 'FailedTargetsSummary', 'ServiceInfo', 'ServiceTopology', 'TargetInfo',
66
+ # Metadata
67
+ 'CardinalityOverview', 'CardinalitySummary', 'MetricCatalog', 'MetricMetadata',
68
+ 'RuntimeConfig', 'TopCardinalityMetric',
69
+ # Onboarding
70
+ 'InstrumentationSnippet', 'InstrumentationStrategy', 'ScrapeEndpointTestResult',
71
+ # Exporter
72
+ 'ExporterInfo', 'ExporterManifestResult', 'ExporterRecommendation',
73
+ 'ExporterUninstallResult', 'ExporterVerificationResult',
74
+ # Rules
75
+ 'AlertRule', 'RecordingRule', 'RuleGroup', 'RuleGroupList',
76
+ 'RuleValidationResult', 'RuleTestResult',
77
+ 'FiringSimulationResult', 'FiringHistoryAnalysis', 'DraftAlertRule',
78
+ ]
79
+