Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/mcp/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- Fixed issue where search results exceeding token limits would be completely discarded instead of returning truncated content. [#604](https://github.com/sourcebot-dev/sourcebot/pull/604)

## [1.0.7] - 2025-10-28
- Updated API client to match the latest Sourcebot release. [#555](https://github.com/sourcebot-dev/sourcebot/pull/555)

Expand Down
16 changes: 16 additions & 0 deletions packages/mcp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,22 @@ server.tool(
const tokens = text.length / 4;

if ((totalTokens + tokens) > maxTokens) {
// Calculate remaining token budget
const remainingTokens = maxTokens - totalTokens;

if (remainingTokens > 100) { // Only truncate if meaningful space left
// Truncate text to fit remaining tokens (tokens ≈ chars/4)
const maxLength = Math.floor(remainingTokens * 4);
const truncatedText = text.substring(0, maxLength) + "\n\n...[content truncated due to token limit]";

content.push({
type: "text",
text: truncatedText,
});

totalTokens += remainingTokens;
}

isResponseTruncated = true;
break;
}
Expand Down
Loading