usage_examples.md•4.72 kB
# GCP MCP Server Usage Examples
This document provides examples of how to use the GCP MCP server with AI assistants like Claude Code.
## Setup
1. Configure the MCP server in your AI assistant's configuration:
```json
{
"mcpServers": {
"gcp": {
"command": "python",
"args": ["-m", "gcp_mcp.cli"],
"env": {
"GCP_PROJECT": "your-project-id",
"GOOGLE_APPLICATION_CREDENTIALS": "/path/to/service-account.json"
}
}
}
}
```
2. Restart your AI assistant to load the MCP server.
## Available Tools
### 1. Query Logs
Query GCP Cloud Logging for specific log entries:
**Example prompts for Claude Code:**
- "Show me error logs from the last hour"
- "Find all logs with severity ERROR from my-service in the last 6 hours"
- "Query logs with filter 'resource.type=\"k8s_container\"' from yesterday"
**Tool parameters:**
```json
{
"project_id": "my-project",
"filter": "severity>=ERROR",
"start_time": "1h",
"limit": 50
}
```
### 2. Analyze Error Logs
Analyze error patterns and trends:
**Example prompts:**
- "Analyze error logs for my-microservice from the last day"
- "What are the most common errors in the last 2 hours?"
- "Show me error patterns for kubernetes containers"
**Tool parameters:**
```json
{
"service_name": "my-microservice",
"time_range": "1d",
"severity": "ERROR"
}
```
### 3. Get Recent Errors
Get the most recent error logs for immediate troubleshooting:
**Example prompts:**
- "Show me the 10 most recent errors"
- "What are the latest errors from GCE instances?"
- "Get recent critical errors"
**Tool parameters:**
```json
{
"count": 10,
"resource_type": "gce_instance"
}
```
### 4. Search Logs by Message
Search for specific text in log messages:
**Example prompts:**
- "Find logs mentioning 'database connection failed'"
- "Search for 'OutOfMemoryError' in the last 2 hours"
- "Look for logs containing 'user authentication' (case sensitive)"
**Tool parameters:**
```json
{
"search_term": "database connection failed",
"time_range": "2h",
"case_sensitive": false
}
```
## Common Use Cases
### 1. Incident Response
**Scenario:** Production service is throwing errors
**Prompts to try:**
1. "Show me the most recent errors from production"
2. "Analyze error patterns in the last hour"
3. "Find logs mentioning 'timeout' or 'connection' errors"
### 2. Performance Investigation
**Scenario:** Investigating slow response times
**Prompts to try:**
1. "Search for logs containing 'slow query' in the last 4 hours"
2. "Show me warning and error logs from the database service"
3. "Analyze logs for performance-related issues"
### 3. Deployment Troubleshooting
**Scenario:** New deployment is causing issues
**Prompts to try:**
1. "Get recent errors from kubernetes containers"
2. "Find logs with severity ERROR since deployment time"
3. "Search for logs mentioning 'failed to start' or 'initialization'"
### 4. Security Monitoring
**Scenario:** Checking for security-related events
**Prompts to try:**
1. "Search for logs containing 'authentication failed'"
2. "Find logs mentioning 'unauthorized' or 'forbidden'"
3. "Show me recent security-related warnings"
## Advanced Filtering
The `query_logs` tool supports Cloud Logging's advanced filter syntax:
### Time-based filters
- `timestamp>="2024-01-01T00:00:00Z"`
- `timestamp>="2024-01-01T00:00:00Z" AND timestamp<="2024-01-01T23:59:59Z"`
### Severity filters
- `severity>=ERROR`
- `severity="WARNING"`
- `severity>="WARNING"`
### Resource filters
- `resource.type="k8s_container"`
- `resource.type="gce_instance"`
- `resource.labels.namespace_name="production"`
### Text content filters
- `textPayload:"connection failed"`
- `jsonPayload.message:"database error"`
### Combined filters
```
severity>=ERROR AND resource.type="k8s_container" AND timestamp>="2024-01-01T00:00:00Z"
```
## Troubleshooting
### Common Issues
1. **"Authentication failed"**
- Check that `GOOGLE_APPLICATION_CREDENTIALS` points to a valid service account file
- Ensure the service account has `roles/logging.viewer` permission
2. **"No project ID found"**
- Set the `GCP_PROJECT` environment variable
- Or specify `project_id` in each tool call
3. **"No logs found"**
- Check the time range - logs might be outside the specified period
- Verify the filter syntax
- Ensure the service account has access to the specified project
### Debug Mode
To enable debug logging:
```json
{
"mcpServers": {
"gcp": {
"command": "python",
"args": ["-m", "gcp_mcp.cli", "--debug"],
"env": {
"GCP_PROJECT": "your-project-id",
"GOOGLE_APPLICATION_CREDENTIALS": "/path/to/service-account.json"
}
}
}
}
```