수정 버전111

This commit is contained in:
jade
2026-07-16 18:03:59 +09:00
parent 1a572351c3
commit 9b0ed9dd51
2 changed files with 59 additions and 0 deletions

View File

@@ -87,6 +87,38 @@ public class CustomWebMvcSseServerTransportProvider implements McpServerTranspor
return emitter;
}
public org.springframework.web.servlet.mvc.method.annotation.SseEmitter handleCustomSse(String sessionId, String body) {
if (sessionFactory == null) {
throw new IllegalStateException("SessionFactory not configured");
}
org.springframework.web.servlet.mvc.method.annotation.SseEmitter emitter = new org.springframework.web.servlet.mvc.method.annotation.SseEmitter(-1L);
CustomMcpSessionTransport sessionTransport = new CustomMcpSessionTransport(emitter, sessionId);
McpServerSession session = sessionFactory.create(sessionTransport);
sessions.put(sessionId, session);
emitter.onCompletion(() -> sessions.remove(sessionId));
emitter.onTimeout(() -> sessions.remove(sessionId));
new Thread(() -> {
try {
// 커스텀 클라이언트는 endpoint 이벤트를 무시할 수 있지만, 표준 호환성을 위해 전송
Thread.sleep(100);
emitter.send(org.springframework.web.servlet.mvc.method.annotation.SseEmitter.event().name("endpoint").data(messageEndpoint + "?sessionId=" + sessionId));
// Body로 들어온 initialize 등 즉시 처리
if (body != null && !body.trim().isEmpty()) {
handleMessage(sessionId, body);
}
} catch (Exception e) {
emitter.completeWithError(e);
}
}).start();
return emitter;
}
public org.springframework.http.ResponseEntity<String> handleMessage(String sessionId, String body) {
log.info("Received POST message for sessionId: " + sessionId + ", body: " + body);
if (sessionId == null || !sessions.containsKey(sessionId)) {

View File

@@ -5,6 +5,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
@@ -57,4 +58,30 @@ public class DynamicMcpController {
}
return transport.handleMessage(sessionId, body);
}
@PostMapping("/mcp/custom/{category}")
public ResponseEntity<?> handleCustomMcp(
@PathVariable("category") String category,
@RequestHeader(value = "Mcp-Session-Id", required = false) String sessionId,
@RequestBody(required = false) String body) {
CustomWebMvcSseServerTransportProvider transport = manager.getTransport(category);
if (transport == null) {
return ResponseEntity.badRequest().body("Unknown category: " + category);
}
if (sessionId == null || sessionId.isEmpty()) {
// 새 세션 생성 (initialize 요청)
String newSessionId = java.util.UUID.randomUUID().toString();
SseEmitter emitter = transport.handleCustomSse(newSessionId, body);
return ResponseEntity.ok()
.header("Mcp-Session-Id", newSessionId)
.body(emitter);
} else {
// 기존 세션 메시지 전송 (tools/call 등)
transport.handleMessage(sessionId, body);
return ResponseEntity.accepted().build();
}
}
}