mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-14 18:05:27 +07:00
[MCP Server] Rename McpCallAdditionalData to McpCallInfo and related usages
(cherry picked from commit 3bd921f442fe965d33d5b473b289f9f630772d71) GitOrigin-RevId: fa74ac76f40672899e2dc3322f5ff8d307a39659
This commit is contained in:
committed by
intellij-monorepo-bot
parent
7c7a9b1ce4
commit
ef2aee0ce4
@@ -6,7 +6,7 @@ import org.jetbrains.ide.RestService.Companion.getLastFocusedOrOpenedProject
|
||||
import kotlin.coroutines.AbstractCoroutineContextElement
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
class McpCallAdditionalData(
|
||||
class McpCallInfo(
|
||||
val callId: Int,
|
||||
val clientInfo: ClientInfo,
|
||||
val project: Project?,
|
||||
@@ -21,23 +21,23 @@ class McpCallAdditionalData(
|
||||
|
||||
class ClientInfo(val name: String, val version: String)
|
||||
|
||||
class McpCallAdditionalDataElement(val additionalData: McpCallAdditionalData) : AbstractCoroutineContextElement(Key) {
|
||||
class McpCallAdditionalDataElement(val additionalData: McpCallInfo) : AbstractCoroutineContextElement(Key) {
|
||||
companion object Key : CoroutineContext.Key<McpCallAdditionalDataElement>
|
||||
}
|
||||
|
||||
val CoroutineContext.mcpCallAdditionalDataOrNull: McpCallAdditionalData? get() = get(McpCallAdditionalDataElement)?.additionalData
|
||||
val CoroutineContext.mcpCallAdditionalData: McpCallAdditionalData get() = mcpCallAdditionalDataOrNull ?: error("mcpCallAdditionalData called outside of a MCP call")
|
||||
val CoroutineContext.mcpCallInfoOrNull: McpCallInfo? get() = get(McpCallAdditionalDataElement)?.additionalData
|
||||
val CoroutineContext.mcpCallInfo: McpCallInfo get() = mcpCallInfoOrNull ?: error("mcpCallAdditionalData called outside of a MCP call")
|
||||
|
||||
/**
|
||||
* Returns information about the MCP client that is calling a tool.
|
||||
*/
|
||||
val CoroutineContext.clientInfo: ClientInfo get() = mcpCallAdditionalData.clientInfo
|
||||
val CoroutineContext.clientInfo: ClientInfo get() = mcpCallInfo.clientInfo
|
||||
|
||||
|
||||
/**
|
||||
* Returns information about the MCP tool that is called.
|
||||
*/
|
||||
val CoroutineContext.currentToolDescriptor: McpToolDescriptor get() = mcpCallAdditionalData.mcpToolDescriptor
|
||||
val CoroutineContext.currentToolDescriptor: McpToolDescriptor get() = mcpCallInfo.mcpToolDescriptor
|
||||
|
||||
/**
|
||||
* MCP tool can resolve a project with this extension property. In the case of running some MCP clients (like Claude) by IJ infrastructure
|
||||
@@ -58,7 +58,7 @@ val CoroutineContext.project: Project
|
||||
* The same as [projectOrNull], but allows to specify whether to look for any/last focused project or take only the one from the context element
|
||||
*/
|
||||
fun CoroutineContext.getProjectOrNull(lookForAnyProject: Boolean): Project? {
|
||||
val projectFromContext = mcpCallAdditionalData.project
|
||||
val projectFromContext = mcpCallInfo.project
|
||||
if (projectFromContext != null) return projectFromContext
|
||||
if (!lookForAnyProject) return null
|
||||
return getLastFocusedOrOpenedProject()
|
||||
@@ -12,11 +12,11 @@ interface ToolCallListener {
|
||||
val TOPIC: Topic<ToolCallListener> = Topic(ToolCallListener::class.java)
|
||||
}
|
||||
|
||||
fun beforeMcpToolCall(mcpToolDescriptor: McpToolDescriptor, additionalData: McpCallAdditionalData) {}
|
||||
fun beforeMcpToolCall(mcpToolDescriptor: McpToolDescriptor, additionalData: McpCallInfo) {}
|
||||
|
||||
fun afterMcpToolCall(mcpToolDescriptor: McpToolDescriptor, events: List<McpToolSideEffectEvent>, error: Throwable?, additionalData: McpCallAdditionalData) {}
|
||||
fun afterMcpToolCall(mcpToolDescriptor: McpToolDescriptor, events: List<McpToolSideEffectEvent>, error: Throwable?, callInfo: McpCallInfo) {}
|
||||
|
||||
fun toolActivity(mcpToolDescriptor: McpToolDescriptor, @NlsContexts.Label toolActivityDescription: String, additionalData: McpCallAdditionalData) {}
|
||||
fun toolActivity(mcpToolDescriptor: McpToolDescriptor, @NlsContexts.Label toolActivityDescription: String, callInfo: McpCallInfo) {}
|
||||
}
|
||||
|
||||
sealed interface McpToolSideEffectEvent
|
||||
@@ -29,5 +29,5 @@ class FileMovedEvent(val file: VirtualFile, val oldParent: VirtualFile, val newP
|
||||
class FileContentChangeEvent(val file: VirtualFile, val oldContent: String?, val newContent: String) : FileEvent
|
||||
|
||||
fun CoroutineContext.reportToolActivity(@NlsContexts.Label toolDescription: String) {
|
||||
application.messageBus.syncPublisher(ToolCallListener.TOPIC).toolActivity(this.currentToolDescriptor, toolDescription, this.mcpCallAdditionalData)
|
||||
application.messageBus.syncPublisher(ToolCallListener.TOPIC).toolActivity(this.currentToolDescriptor, toolDescription, this.mcpCallInfo)
|
||||
}
|
||||
@@ -205,7 +205,7 @@ private fun McpTool.mcpToolToRegisteredTool(server: Server, projectPathFromIniti
|
||||
val initialDocumentContents = ConcurrentHashMap<Document, String>()
|
||||
val clientVersion = server.clientVersion ?: Implementation("Unknown MCP client", "Unknown version")
|
||||
|
||||
val additionalData = McpCallAdditionalData(
|
||||
val additionalData = McpCallInfo(
|
||||
callId = callId.getAndAdd(1),
|
||||
clientInfo = ClientInfo(clientVersion.name, clientVersion.version),
|
||||
project = project,
|
||||
@@ -217,9 +217,9 @@ private fun McpTool.mcpToolToRegisteredTool(server: Server, projectPathFromIniti
|
||||
val callResult = coroutineScope {
|
||||
|
||||
VirtualFileManager.getInstance().addAsyncFileListener(this, AsyncFileListener { events ->
|
||||
val inHandlerData = currentThreadContext().mcpCallAdditionalDataOrNull
|
||||
if (inHandlerData != null && inHandlerData.callId == additionalData.callId) {
|
||||
logger.trace { "VFS changes detected for call: $inHandlerData" }
|
||||
val inHandlerInfo = currentThreadContext().mcpCallInfoOrNull
|
||||
if (inHandlerInfo != null && inHandlerInfo.callId == additionalData.callId) {
|
||||
logger.trace { "VFS changes detected for call: $inHandlerInfo" }
|
||||
vfsEvent.addAll(events)
|
||||
}
|
||||
// probably we have to read initial contents here
|
||||
@@ -230,9 +230,9 @@ private fun McpTool.mcpToolToRegisteredTool(server: Server, projectPathFromIniti
|
||||
val documentListener = object : DocumentListener {
|
||||
// record content before any change
|
||||
override fun beforeDocumentChange(event: DocumentEvent) {
|
||||
val inHandlerData = currentThreadContext().mcpCallAdditionalDataOrNull
|
||||
if (inHandlerData != null && inHandlerData.callId == additionalData.callId) {
|
||||
logger.trace { "Document changes detected for call: $inHandlerData" }
|
||||
val inHandlerInfo = currentThreadContext().mcpCallInfoOrNull
|
||||
if (inHandlerInfo != null && inHandlerInfo.callId == additionalData.callId) {
|
||||
logger.trace { "Document changes detected for call: $inHandlerInfo" }
|
||||
initialDocumentContents.computeIfAbsent(event.document) { event.document.text }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user