Skip to content

Commit a0ab066

Browse files
authored
Optimize workflows based on live log analysis: MCP pagination and JSON parsing (#3293)
1 parent 8342771 commit a0ab066

5 files changed

Lines changed: 334 additions & 4 deletions

File tree

‎.github/workflows/cli-version-checker.lock.yml‎

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.github/workflows/cli-version-checker.md‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,55 @@ Template structure:
125125
- Test with `make recompile` before creating PR
126126
- **DO NOT COMMIT** `*.lock.yml` or `pkg/workflow/js/*.js` files directly
127127

128+
## Common JSON Parsing Issues
129+
130+
When using npm commands or other CLI tools, their output may include informational messages with Unicode symbols that break JSON parsing:
131+
132+
**Problem Patterns**:
133+
- `Unexpected token 'ℹ', "ℹ Timeout "... is not valid JSON`
134+
- `Unexpected token '⚠', "⚠ pip pack"... is not valid JSON`
135+
- `Unexpected token '✓', "✓ Success"... is not valid JSON`
136+
137+
**Solutions**:
138+
139+
### 1. Filter stderr (Recommended)
140+
Redirect stderr to suppress npm warnings/info:
141+
```bash
142+
npm view @github/copilot version 2>/dev/null
143+
npm view @anthropic-ai/claude-code --json 2>/dev/null
144+
```
145+
146+
### 2. Use grep to filter output
147+
Remove lines with Unicode symbols before parsing:
148+
```bash
149+
npm view @github/copilot --json | grep -v "^[ℹ⚠✓]"
150+
```
151+
152+
### 3. Use jq for reliable extraction
153+
Let jq handle malformed input:
154+
```bash
155+
# Extract version field only, ignoring non-JSON lines
156+
npm view @github/copilot --json 2>/dev/null | jq -r '.version'
157+
```
158+
159+
### 4. Check tool output before parsing
160+
Always validate JSON before attempting to parse:
161+
```bash
162+
output=$(npm view package --json 2>/dev/null)
163+
if echo "$output" | jq empty 2>/dev/null; then
164+
# Valid JSON, safe to parse
165+
version=$(echo "$output" | jq -r '.version')
166+
else
167+
# Invalid JSON, handle error
168+
echo "Warning: npm output is not valid JSON"
169+
fi
170+
```
171+
172+
**Best Practice**: Combine stderr filtering with jq extraction for most reliable results:
173+
```bash
174+
npm view @github/copilot --json 2>/dev/null | jq -r '.version'
175+
```
176+
128177
## Error Handling
129178
- **SAVE PROGRESS**: Before exiting on errors, save current state to cache-memory
130179
- **RESUME ON RESTART**: Check cache-memory on startup to resume from where you left off
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
## MCP Response Size Limits
2+
3+
MCP tool responses have a **25,000 token limit**. When GitHub API responses exceed this limit, workflows must retry with pagination parameters, wasting turns and tokens.
4+
5+
### Common Scenarios
6+
7+
**Problem**: Fetching large result sets without pagination
8+
- `list_pull_requests` with many PRs (75,897 tokens in one case)
9+
- `pull_request_read` with large diff/comments (31,675 tokens observed)
10+
- `search_issues`, `search_code` with many results
11+
12+
**Solution**: Use proactive pagination to stay under token limits
13+
14+
### Pagination Best Practices
15+
16+
#### 1. Use `perPage` Parameter
17+
18+
Limit results per request to prevent oversized responses:
19+
20+
```bash
21+
# Good: Fetch PRs in small batches
22+
list_pull_requests --perPage 10
23+
24+
# Good: Get issue with limited comments
25+
issue_read --method get_comments --perPage 20
26+
27+
# Bad: Default pagination may return too much data
28+
list_pull_requests # May exceed 25k tokens
29+
```
30+
31+
#### 2. Common `perPage` Values
32+
33+
- **10-20**: For detailed items (PRs with diffs, issues with comments)
34+
- **50-100**: For simpler list operations (commits, branches, labels)
35+
- **1-5**: For exploratory queries or schema discovery
36+
37+
#### 3. Handle Pagination Loops
38+
39+
When you need all results:
40+
41+
```bash
42+
# Step 1: Fetch first page
43+
result=$(list_pull_requests --perPage 20 --page 1)
44+
45+
# Step 2: Check if more pages exist
46+
# Most list operations return metadata about total count or next page
47+
48+
# Step 3: Fetch subsequent pages if needed
49+
result=$(list_pull_requests --perPage 20 --page 2)
50+
```
51+
52+
### Tool-Specific Guidance
53+
54+
#### Pull Requests
55+
56+
```bash
57+
# Fetch recent PRs in small batches
58+
list_pull_requests --state all --perPage 10 --sort updated --direction desc
59+
60+
# Get PR details without full diff/comments
61+
pull_request_read --method get --pullNumber 123
62+
63+
# Get PR files separately if needed
64+
pull_request_read --method get_files --pullNumber 123 --perPage 30
65+
```
66+
67+
#### Issues
68+
69+
```bash
70+
# List issues with pagination
71+
list_issues --perPage 20 --page 1
72+
73+
# Get issue comments in batches
74+
issue_read --method get_comments --issue_number 123 --perPage 20
75+
```
76+
77+
#### Code Search
78+
79+
```bash
80+
# Search with limited results
81+
search_code --query "function language:go" --perPage 10
82+
```
83+
84+
### Error Messages to Watch For
85+
86+
If you see these errors, add pagination:
87+
88+
- `MCP tool "list_pull_requests" response (75897 tokens) exceeds maximum allowed tokens (25000)`
89+
- `MCP tool "pull_request_read" response (31675 tokens) exceeds maximum allowed tokens (25000)`
90+
- `Response too large for tool [tool_name]`
91+
92+
### Performance Tips
93+
94+
1. **Start small**: Use `perPage: 10` initially, increase if needed
95+
2. **Fetch incrementally**: Get overview first, then details for specific items
96+
3. **Avoid wildcards**: Don't fetch all data when you need specific items
97+
4. **Use filters**: Combine `perPage` with state/label/date filters to reduce results
98+
99+
### Example Workflow Pattern
100+
101+
```markdown
102+
# Analyze Recent Pull Requests
103+
104+
1. Fetch 10 most recent PRs (stay under token limit)
105+
2. For each PR, get summary without full diff
106+
3. If detailed analysis needed, fetch files for specific PR separately
107+
4. Process results incrementally rather than loading everything at once
108+
```
109+
110+
This proactive approach eliminates retry loops and reduces token consumption.

‎.github/workflows/smoke-claude.lock.yml‎

Lines changed: 120 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)