Skip to content

Commit 5e35e08

Browse files
Saqooshaclaude
andcommitted
refactor: simplify console logs response format based on PR feedback
- Remove pagination object and consolidate info into message field - Delete deprecated GetAllLogsAsJson method - Optimize log counting with single loop for better performance - Add default offset/limit values to resource templates - Simplify response to only include logs, message, and success fields - Move count information (total, filtered, returned) into message text - Add parameter validation for offset/limit in TypeScript This addresses all review comments from PR #42 to make the response format more concise and easier for AI agents to process. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 2ca51d7 commit 5e35e08

File tree

4 files changed

+44
-54
lines changed

4 files changed

+44
-54
lines changed

Editor/Resources/GetConsoleLogsResource.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using Newtonsoft.Json.Linq;
23
using McpUnity.Services;
34

@@ -35,14 +36,19 @@ public override JObject Fetch(JObject parameters)
3536
// Use the new paginated method
3637
JObject result = _consoleLogsService.GetLogsAsJson(logType, offset, limit);
3738

38-
// Add success info to the response
39+
// Add formatted message with pagination info
40+
string typeFilter = logType != null ? $" of type '{logType}'" : "";
41+
int returnedCount = result["_returnedCount"]?.Value<int>() ?? 0;
42+
int filteredCount = result["_filteredCount"]?.Value<int>() ?? 0;
43+
int totalCount = result["_totalCount"]?.Value<int>() ?? 0;
44+
45+
result["message"] = $"Retrieved {returnedCount} of {filteredCount} log entries{typeFilter} (offset: {offset}, limit: {limit}, total: {totalCount})";
3946
result["success"] = true;
4047

41-
var pagination = result["pagination"] as JObject;
42-
string typeFilter = logType != null ? $" of type '{logType}'" : "";
43-
int returnedCount = pagination?["returnedCount"]?.Value<int>() ?? 0;
44-
int filteredCount = pagination?["filteredCount"]?.Value<int>() ?? 0;
45-
result["message"] = $"Retrieved {returnedCount} of {filteredCount} log entries{typeFilter} (offset: {offset}, limit: {limit})";
48+
// Remove internal count fields (they're now in the message)
49+
result.Remove("_totalCount");
50+
result.Remove("_filteredCount");
51+
result.Remove("_returnedCount");
4652

4753
return result;
4854
}

Editor/Services/ConsoleLogsService.cs

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,6 @@ public void StopListening()
7777
EditorApplication.update -= CheckConsoleClearViaReflection;
7878
#endif
7979
}
80-
81-
/// <summary>
82-
/// Get all logs as a JSON array
83-
/// </summary>
84-
/// <returns>JArray containing all logs</returns>
85-
public JArray GetAllLogsAsJson(string logType = "")
86-
{
87-
var result = GetLogsAsJson(logType, 0, int.MaxValue);
88-
return result["logs"] as JArray;
89-
}
90-
9180
/// <summary>
9281
/// Get logs as a JSON array with pagination support
9382
/// </summary>
@@ -121,23 +110,21 @@ public JObject GetLogsAsJson(string logType = "", int offset = 0, int limit = 10
121110

122111
lock (_logEntries)
123112
{
124-
// First pass: count total and filtered entries
125-
foreach (var entry in _logEntries)
126-
{
127-
totalCount++;
128-
if (!filter || unityLogTypes.Contains(entry.Type.ToString()))
129-
{
130-
filteredCount++;
131-
}
132-
}
113+
totalCount = _logEntries.Count;
133114

134-
// Second pass: collect the requested page (newest first)
115+
// Single pass: count filtered entries and collect the requested page (newest first)
135116
for (int i = _logEntries.Count - 1; i >= 0; i--)
136117
{
137118
var entry = _logEntries[i];
119+
120+
// Skip if filtering and entry doesn't match the filter
138121
if (filter && !unityLogTypes.Contains(entry.Type.ToString()))
139122
continue;
140-
123+
124+
// Count filtered entries
125+
filteredCount++;
126+
127+
// Check if we're in the offset range and haven't reached the limit yet
141128
if (currentIndex >= offset && logsArray.Count < limit)
142129
{
143130
logsArray.Add(new JObject
@@ -150,22 +137,18 @@ public JObject GetLogsAsJson(string logType = "", int offset = 0, int limit = 10
150137
}
151138

152139
currentIndex++;
153-
if (logsArray.Count >= limit) break;
140+
141+
// Early exit if we've collected enough logs
142+
if (currentIndex >= offset + limit) break;
154143
}
155144
}
156145

157146
return new JObject
158147
{
159148
["logs"] = logsArray,
160-
["pagination"] = new JObject
161-
{
162-
["offset"] = offset,
163-
["limit"] = limit,
164-
["totalCount"] = totalCount,
165-
["filteredCount"] = filteredCount,
166-
["returnedCount"] = logsArray.Count,
167-
["hasMore"] = offset + logsArray.Count < filteredCount
168-
}
149+
["_totalCount"] = totalCount,
150+
["_filteredCount"] = filteredCount,
151+
["_returnedCount"] = logsArray.Count
169152
};
170153
}
171154

Editor/Services/IConsoleLogsService.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ namespace McpUnity.Services
1010
/// </summary>
1111
public interface IConsoleLogsService
1212
{
13-
/// <summary>
14-
/// Get all logs as a JSON array, optionally filtered by log type
15-
/// </summary>
16-
/// <param name="logType">UnityEngine.LogType as string (e.g. "Error", "Warning", "Log"). Empty string for all logs.</param>
17-
/// <returns>JArray containing filtered logs</returns>
18-
JArray GetAllLogsAsJson(string logType = "");
19-
2013
/// <summary>
2114
/// Get logs as a JSON object with pagination support
2215
/// </summary>

Server~/src/resources/getConsoleLogsResource.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,27 @@ function listLogTypes(resourceMimeType: string) {
1717
return {
1818
resources: [
1919
{
20-
uri: `unity://logs/`,
20+
uri: `unity://logs/?offset=0&limit=50`,
2121
name: "All logs",
22-
description: "Retrieve Unity console logs (newest first). Use pagination to avoid token limits: ?offset=0&limit=50 for recent logs. Default limit=100 may be too large for LLM context.",
22+
description: "Retrieve Unity console logs (newest first). Default pagination offset=0&limit=50 to avoid token limits.",
2323
mimeType: resourceMimeType
2424
},
2525
{
26-
uri: `unity://logs/error`,
26+
uri: `unity://logs/error?offset=0&limit=20`,
2727
name: "Error logs",
28-
description: "Retrieve only error logs from Unity console (newest first). Use ?offset=0&limit=20 to avoid token limits. Large log sets may exceed LLM context window.",
28+
description: "Retrieve only error logs from Unity console (newest first). Default pagination offset=0&limit=20.",
2929
mimeType: resourceMimeType
3030
},
3131
{
32-
uri: `unity://logs/warning`,
32+
uri: `unity://logs/warning?offset=0&limit=30`,
3333
name: "Warning logs",
34-
description: "Retrieve only warning logs from Unity console (newest first). Use pagination ?offset=0&limit=30 to manage token usage effectively.",
34+
description: "Retrieve only warning logs from Unity console (newest first). Default pagination offset=0&limit=30.",
3535
mimeType: resourceMimeType
3636
},
3737
{
38-
uri: `unity://logs/info`,
38+
uri: `unity://logs/info?offset=0&limit=25`,
3939
name: "Info logs",
40-
description: "Retrieve only info logs from Unity console (newest first). Use smaller limits like ?limit=25 to prevent token overflow in LLM responses.",
40+
description: "Retrieve only info logs from Unity console (newest first). Default pagination offset=0&limit=25.",
4141
mimeType: resourceMimeType
4242
}
4343
]
@@ -76,9 +76,17 @@ async function resourceHandler(mcpUnity: McpUnity, uri: URL, variables: Variable
7676
let logType = variables["logType"] ? decodeURIComponent(variables["logType"] as string) : undefined;
7777
if (logType === '') logType = undefined;
7878

79-
// Extract pagination parameters
79+
// Extract pagination parameters with validation
8080
const offset = variables["offset"] ? parseInt(variables["offset"] as string, 10) : 0;
8181
const limit = variables["limit"] ? parseInt(variables["limit"] as string, 10) : 100;
82+
83+
// Validate pagination parameters
84+
if (isNaN(offset) || offset < 0) {
85+
throw new McpUnityError(ErrorType.VALIDATION, 'Invalid offset parameter: must be a non-negative integer');
86+
}
87+
if (isNaN(limit) || limit <= 0) {
88+
throw new McpUnityError(ErrorType.VALIDATION, 'Invalid limit parameter: must be a positive integer');
89+
}
8290

8391
// Send request to Unity
8492
const response = await mcpUnity.sendRequest({

0 commit comments

Comments
 (0)