Skip to content

Commit 1aac20d

Browse files
committed
tools: fix CleanupSession error handling and move API key check to main
1 parent 532bc7d commit 1aac20d

3 files changed

Lines changed: 75 additions & 10 deletions

File tree

main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@ func init() {
5858
})))
5959

6060
tlAPIBaseURL = cmp.Or(tlAPIBaseURL, "https://api.tensorlake.ai/documents/v2")
61+
}
62+
63+
func main() {
6164
if tlAPIKey == "" {
6265
slog.Error("TENSORLAKE_API_KEY environment variable is required")
6366
os.Exit(1)
6467
}
65-
}
6668

67-
func main() {
6869
impl := mcp.NewServer(&mcp.Implementation{
6970
Name: serverName,
7071
Version: serverVersion,

tools.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -473,22 +473,22 @@ func newToolResultError(err error) (*mcp.CallToolResult, any, error) {
473473

474474
func (s *server) CleanupSession(ctx context.Context) {
475475
files.Range(func(key string, value *FileInfo) bool {
476-
err := s.tl.DeleteFile(ctx, key)
477-
if err != nil {
478-
slog.Error("failed to delete document", "document_id", key, "error", err)
479-
return false
480-
}
481-
slog.Info("document deleted", "document_id", key)
482-
483476
for _, parseJob := range value.ParseJobs {
484477
err := s.tl.DeleteParseJob(ctx, parseJob.ParseId)
485478
if err != nil {
486479
slog.Error("failed to delete parse job", "parse_id", parseJob.ParseId, "error", err)
487-
return false
480+
continue
488481
}
489482
slog.Info("parse job deleted", "parse_id", parseJob.ParseId)
490483
}
491484

485+
err := s.tl.DeleteFile(ctx, key)
486+
if err != nil {
487+
slog.Error("failed to delete document", "document_id", key, "error", err)
488+
return true // continue to next document
489+
}
490+
files.Delete(key)
491+
slog.Info("document deleted", "document_id", key)
492492
return true
493493
})
494494
}

tools_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,67 @@ func TestUploadParseDelete(t *testing.T) {
125125
t.Error("expected document to be removed from files map after delete")
126126
}
127127
}
128+
129+
// TestCleanupSession uploads two documents, parses them, then calls
130+
// CleanupSession and verifies all documents and parse jobs are cleaned up.
131+
// This also verifies that cleanup continues past individual errors
132+
// (best-effort) rather than stopping on the first failure.
133+
func TestCleanupSession(t *testing.T) {
134+
s := initTestServer(t)
135+
ctx := context.Background()
136+
137+
// Upload two documents.
138+
var docIDs []string
139+
for range 2 {
140+
result, _, err := s.UploadDocument(ctx, &mcp.CallToolRequest{}, &UploadDocumentInput{
141+
URL: "file://testdata/sixt_DE_de.pdf",
142+
})
143+
if err != nil {
144+
t.Fatalf("UploadDocument error: %v", err)
145+
}
146+
if result.IsError {
147+
t.Fatalf("UploadDocument tool error: %v", result.Content)
148+
}
149+
upload := unmarshalToolResult[UploadDocumentOutput](t, result)
150+
docIDs = append(docIDs, upload.DocumentId)
151+
}
152+
153+
// Parse both documents synchronously.
154+
for _, docID := range docIDs {
155+
result, _, err := s.ParseDocument(ctx, &mcp.CallToolRequest{}, &ParseDocumentInput{
156+
DocumentId: docID,
157+
Sync: true,
158+
})
159+
if err != nil {
160+
t.Fatalf("ParseDocument error: %v", err)
161+
}
162+
if result.IsError {
163+
t.Fatalf("ParseDocument tool error: %v", result.Content)
164+
}
165+
}
166+
167+
// Verify both documents are in the files map with parse jobs.
168+
for _, docID := range docIDs {
169+
info, ok := files.Load(docID)
170+
if !ok {
171+
t.Fatalf("expected document %s in files map before cleanup", docID)
172+
}
173+
if len(info.ParseJobs) == 0 {
174+
t.Fatalf("expected parse jobs for document %s before cleanup", docID)
175+
}
176+
}
177+
178+
// Run cleanup.
179+
s.CleanupSession(ctx)
180+
181+
// Verify the files map is empty — all documents should have been
182+
// processed regardless of individual errors.
183+
var remaining []string
184+
files.Range(func(key string, _ *FileInfo) bool {
185+
remaining = append(remaining, key)
186+
return true
187+
})
188+
if len(remaining) > 0 {
189+
t.Errorf("expected files map to be empty after CleanupSession, still has: %v", remaining)
190+
}
191+
}

0 commit comments

Comments
 (0)