Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
import java.util.Scanner;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import software.amazon.smithy.java.core.schema.SerializableStruct;
import software.amazon.smithy.java.io.ByteBufferUtils;
import software.amazon.smithy.java.json.JsonCodec;
import software.amazon.smithy.java.json.JsonSettings;
import software.amazon.smithy.java.logging.InternalLogger;
import software.amazon.smithy.java.mcp.model.JsonRpcRequest;
import software.amazon.smithy.java.mcp.model.JsonRpcResponse;
import software.amazon.smithy.java.server.Server;
import software.amazon.smithy.java.server.Service;
import software.amazon.smithy.utils.SmithyUnstableApi;
Expand Down Expand Up @@ -81,9 +82,9 @@ private void handleRequest(JsonRpcRequest req) {
}
}

var response = mcpService.handleRequest(req, this::writeResponse, protocolVersion);
var response = mcpService.handleRequest(req, this::writeStructToOutput, protocolVersion);
if (response != null) {
writeResponse(response);
writeStructToOutput(response);
}
}

Expand All @@ -108,43 +109,35 @@ public void addNewService(String id, Service service) {
}

public void addNewProxy(McpServerProxy mcpServerProxy) {
mcpService.addNewProxy(mcpServerProxy, this::writeResponse);
mcpService.addNewProxy(mcpServerProxy, this::writeStructToOutput);
refreshTools();
}

public boolean containsMcpServer(String id) {
return mcpService.containsMcpServer(id);
}

private void writeResponse(JsonRpcResponse response) {
private void writeStructToOutput(SerializableStruct shape) {
synchronized (os) {
var bytes = CODEC.serialize(shape);
try {
os.write(CODEC.serializeToString(response).getBytes(StandardCharsets.UTF_8));
if (bytes.hasArray()) {
os.write(bytes.array(), bytes.arrayOffset() + bytes.position(), bytes.remaining());
} else {
os.write(ByteBufferUtils.getBytes(bytes));
}
os.write('\n');
os.flush();
} catch (Exception e) {
LOG.error("Error encoding response", e);
}
}
}

private void writeNotification(JsonRpcRequest notification) {
synchronized (os) {
try {
LOG.debug("Writing notification to stdout: method={}", notification.getMethod());
os.write(CODEC.serializeToString(notification).getBytes(StandardCharsets.UTF_8));
os.write('\n');
os.flush();
} catch (Exception e) {
LOG.error("Error encoding notification", e);
LOG.error("Error writing to output", e);
}
}
}

@Override
public void start() {
// Set up notification writer for proxies
mcpService.setNotificationWriter(this::writeNotification);
mcpService.setNotificationWriter(this::writeStructToOutput);

// Initialize proxies
mcpService.startProxies();
Expand Down