Skip to content

Adjust logging level for frequent / low-level events #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2025
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
16 changes: 8 additions & 8 deletions Sources/MCP/Base/Transports/HTTPClientTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public actor HTTPClientTransport: Transport {
if let continuation = self.initialSessionIDContinuation {
continuation.resume()
self.initialSessionIDContinuation = nil // Consume the continuation
logger.debug("Initial session ID signal triggered for SSE task.")
logger.trace("Initial session ID signal triggered for SSE task.")
}
}

Expand Down Expand Up @@ -251,7 +251,7 @@ public actor HTTPClientTransport: Transport {
logger.warning("SSE responses aren't fully supported on Linux")
messageContinuation.yield(data)
} else if contentType.contains("application/json") {
logger.debug("Received JSON response", metadata: ["size": "\(data.count)"])
logger.trace("Received JSON response", metadata: ["size": "\(data.count)"])
messageContinuation.yield(data)
} else {
logger.warning("Unexpected content type: \(contentType)")
Expand Down Expand Up @@ -285,15 +285,15 @@ public actor HTTPClientTransport: Transport {

if contentType.contains("text/event-stream") {
// For SSE, processing happens via the stream
logger.debug("Received SSE response, processing in streaming task")
logger.trace("Received SSE response, processing in streaming task")
try await self.processSSE(stream)
} else if contentType.contains("application/json") {
// For JSON responses, collect and deliver the data
var buffer = Data()
for try await byte in stream {
buffer.append(byte)
}
logger.debug("Received JSON response", metadata: ["size": "\(buffer.count)"])
logger.trace("Received JSON response", metadata: ["size": "\(buffer.count)"])
messageContinuation.yield(buffer)
} else {
logger.warning("Unexpected content type: \(contentType)")
Expand Down Expand Up @@ -390,7 +390,7 @@ public actor HTTPClientTransport: Transport {

// Wait for the initial session ID signal, but only if sessionID isn't already set
if self.sessionID == nil, let signalTask = self.initialSessionIDSignalTask {
logger.debug("SSE streaming task waiting for initial sessionID signal...")
logger.trace("SSE streaming task waiting for initial sessionID signal...")

// Race the signalTask against a timeout
let timeoutTask = Task {
Expand Down Expand Up @@ -429,14 +429,14 @@ public actor HTTPClientTransport: Transport {
timeoutTask.cancel()

if signalReceived {
logger.debug("SSE streaming task proceeding after initial sessionID signal.")
logger.trace("SSE streaming task proceeding after initial sessionID signal.")
} else {
logger.warning(
"Timeout waiting for initial sessionID signal. SSE stream will proceed (sessionID might be nil)."
)
}
} else if self.sessionID != nil {
logger.debug(
logger.trace(
"Initial sessionID already available. Proceeding with SSE streaming task immediately."
)
} else {
Expand Down Expand Up @@ -525,7 +525,7 @@ public actor HTTPClientTransport: Transport {
// Check if task has been cancelled
if Task.isCancelled { break }

logger.debug(
logger.trace(
"SSE event received",
metadata: [
"type": "\(event.event ?? "message")",
Expand Down
8 changes: 4 additions & 4 deletions Sources/MCP/Base/Transports/NetworkTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ import Logging
})
}

logger.debug("Heartbeat sent")
logger.trace("Heartbeat sent")
}

/// Handles connection failure
Expand Down Expand Up @@ -638,11 +638,11 @@ import Logging

// Check if this is a heartbeat message
if Heartbeat.isHeartbeat(newData) {
logger.debug("Received heartbeat from peer")
logger.trace("Received heartbeat from peer")

// Extract timestamp if available
if let heartbeat = Heartbeat.from(data: newData) {
logger.debug("Heartbeat timestamp: \(heartbeat.timestamp)")
logger.trace("Heartbeat timestamp: \(heartbeat.timestamp)")
}

// Reset the counter since we got valid data
Expand Down Expand Up @@ -783,7 +783,7 @@ import Logging
} else if let content = content {
continuation.resume(returning: content)
} else if isComplete {
self.logger.debug("Connection completed by peer")
self.logger.trace("Connection completed by peer")
continuation.resume(throwing: MCPError.connectionClosed)
} else {
// EOF: Resume with empty data instead of throwing an error
Expand Down
2 changes: 1 addition & 1 deletion Sources/MCP/Base/Transports/StdioTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ import struct Foundation.Data
pendingData = pendingData[(newlineIndex + 1)...]

if !messageData.isEmpty {
logger.debug(
logger.trace(
"Message received", metadata: ["size": "\(messageData.count)"])
messageContinuation.yield(Data(messageData))
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/MCP/Client/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ public actor Client {
// MARK: -

private func handleResponse(_ response: Response<AnyMethod>) async {
await logger?.debug(
await logger?.trace(
"Processing response",
metadata: ["id": "\(response.id)"])

Expand All @@ -684,7 +684,7 @@ public actor Client {
}

private func handleMessage(_ message: Message<AnyNotification>) async {
await logger?.debug(
await logger?.trace(
"Processing notification",
metadata: ["method": "\(message.method)"])

Expand Down Expand Up @@ -728,7 +728,7 @@ public actor Client {

// Add handler for batch responses
private func handleBatchResponse(_ responses: [AnyResponse]) async {
await logger?.debug("Processing batch response", metadata: ["count": "\(responses.count)"])
await logger?.trace("Processing batch response", metadata: ["count": "\(responses.count)"])
for response in responses {
// Attempt to remove the pending request.
// If successful, pendingRequest contains the request.
Expand Down
6 changes: 3 additions & 3 deletions Sources/MCP/Server/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ public actor Server {

/// Process a batch of requests and/or notifications
private func handleBatch(_ batch: Batch) async throws {
await logger?.debug("Processing batch request", metadata: ["size": "\(batch.items.count)"])
await logger?.trace("Processing batch request", metadata: ["size": "\(batch.items.count)"])

if batch.items.isEmpty {
// Empty batch is invalid according to JSON-RPC spec
Expand Down Expand Up @@ -450,7 +450,7 @@ public actor Server {
)
}

await logger?.debug(
await logger?.trace(
"Processing request",
metadata: [
"method": "\(request.method)",
Expand Down Expand Up @@ -505,7 +505,7 @@ public actor Server {
}

private func handleMessage(_ message: Message<AnyNotification>) async throws {
await logger?.debug(
await logger?.trace(
"Processing notification",
metadata: ["method": "\(message.method)"])

Expand Down