[MCP Server] Rename McpCallAdditionalData to McpCallInfo and related usages

(cherry picked from commit 3bd921f442fe965d33d5b473b289f9f630772d71)

GitOrigin-RevId: fa74ac76f40672899e2dc3322f5ff8d307a39659
This commit is contained in:
Artem.Bukhonov
2025-07-18 23:00:21 +02:00
committed by intellij-monorepo-bot
parent 7c7a9b1ce4
commit ef2aee0ce4
3 changed files with 18 additions and 18 deletions

View File

@@ -6,7 +6,7 @@ import org.jetbrains.ide.RestService.Companion.getLastFocusedOrOpenedProject
import kotlin.coroutines.AbstractCoroutineContextElement import kotlin.coroutines.AbstractCoroutineContextElement
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
class McpCallAdditionalData( class McpCallInfo(
val callId: Int, val callId: Int,
val clientInfo: ClientInfo, val clientInfo: ClientInfo,
val project: Project?, val project: Project?,
@@ -21,23 +21,23 @@ class McpCallAdditionalData(
class ClientInfo(val name: String, val version: String) 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> companion object Key : CoroutineContext.Key<McpCallAdditionalDataElement>
} }
val CoroutineContext.mcpCallAdditionalDataOrNull: McpCallAdditionalData? get() = get(McpCallAdditionalDataElement)?.additionalData val CoroutineContext.mcpCallInfoOrNull: McpCallInfo? get() = get(McpCallAdditionalDataElement)?.additionalData
val CoroutineContext.mcpCallAdditionalData: McpCallAdditionalData get() = mcpCallAdditionalDataOrNull ?: error("mcpCallAdditionalData called outside of a MCP call") 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. * 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. * 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 * 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 * 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? { fun CoroutineContext.getProjectOrNull(lookForAnyProject: Boolean): Project? {
val projectFromContext = mcpCallAdditionalData.project val projectFromContext = mcpCallInfo.project
if (projectFromContext != null) return projectFromContext if (projectFromContext != null) return projectFromContext
if (!lookForAnyProject) return null if (!lookForAnyProject) return null
return getLastFocusedOrOpenedProject() return getLastFocusedOrOpenedProject()

View File

@@ -12,11 +12,11 @@ interface ToolCallListener {
val TOPIC: Topic<ToolCallListener> = Topic(ToolCallListener::class.java) 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 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 class FileContentChangeEvent(val file: VirtualFile, val oldContent: String?, val newContent: String) : FileEvent
fun CoroutineContext.reportToolActivity(@NlsContexts.Label toolDescription: String) { 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)
} }

View File

@@ -205,7 +205,7 @@ private fun McpTool.mcpToolToRegisteredTool(server: Server, projectPathFromIniti
val initialDocumentContents = ConcurrentHashMap<Document, String>() val initialDocumentContents = ConcurrentHashMap<Document, String>()
val clientVersion = server.clientVersion ?: Implementation("Unknown MCP client", "Unknown version") val clientVersion = server.clientVersion ?: Implementation("Unknown MCP client", "Unknown version")
val additionalData = McpCallAdditionalData( val additionalData = McpCallInfo(
callId = callId.getAndAdd(1), callId = callId.getAndAdd(1),
clientInfo = ClientInfo(clientVersion.name, clientVersion.version), clientInfo = ClientInfo(clientVersion.name, clientVersion.version),
project = project, project = project,
@@ -217,9 +217,9 @@ private fun McpTool.mcpToolToRegisteredTool(server: Server, projectPathFromIniti
val callResult = coroutineScope { val callResult = coroutineScope {
VirtualFileManager.getInstance().addAsyncFileListener(this, AsyncFileListener { events -> VirtualFileManager.getInstance().addAsyncFileListener(this, AsyncFileListener { events ->
val inHandlerData = currentThreadContext().mcpCallAdditionalDataOrNull val inHandlerInfo = currentThreadContext().mcpCallInfoOrNull
if (inHandlerData != null && inHandlerData.callId == additionalData.callId) { if (inHandlerInfo != null && inHandlerInfo.callId == additionalData.callId) {
logger.trace { "VFS changes detected for call: $inHandlerData" } logger.trace { "VFS changes detected for call: $inHandlerInfo" }
vfsEvent.addAll(events) vfsEvent.addAll(events)
} }
// probably we have to read initial contents here // probably we have to read initial contents here
@@ -230,9 +230,9 @@ private fun McpTool.mcpToolToRegisteredTool(server: Server, projectPathFromIniti
val documentListener = object : DocumentListener { val documentListener = object : DocumentListener {
// record content before any change // record content before any change
override fun beforeDocumentChange(event: DocumentEvent) { override fun beforeDocumentChange(event: DocumentEvent) {
val inHandlerData = currentThreadContext().mcpCallAdditionalDataOrNull val inHandlerInfo = currentThreadContext().mcpCallInfoOrNull
if (inHandlerData != null && inHandlerData.callId == additionalData.callId) { if (inHandlerInfo != null && inHandlerInfo.callId == additionalData.callId) {
logger.trace { "Document changes detected for call: $inHandlerData" } logger.trace { "Document changes detected for call: $inHandlerInfo" }
initialDocumentContents.computeIfAbsent(event.document) { event.document.text } initialDocumentContents.computeIfAbsent(event.document) { event.document.text }
} }
} }