Skip to content

Commit df37bbe

Browse files
committed
JBRes-7677: opentelementry support for MCP
1 parent 25a5889 commit df37bbe

File tree

29 files changed

+1461
-227
lines changed

29 files changed

+1461
-227
lines changed

agents/agents-core/src/commonMain/kotlin/ai/koog/agents/core/agent/AIAgent.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public expect abstract class AIAgent<Input, Output> constructor() : Closeable {
9191
promptExecutor: PromptExecutor,
9292
agentConfig: AIAgentConfig,
9393
strategy: AIAgentGraphStrategy<Input, Output>,
94-
toolRegistry: ToolRegistry = ToolRegistry.EMPTY,
94+
toolRegistry: ToolRegistry = ToolRegistry.empty(),
9595
id: String? = null,
9696
clock: Clock = Clock.System,
9797
noinline installFeatures: FeatureContext.() -> Unit = {},
@@ -113,7 +113,7 @@ public expect abstract class AIAgent<Input, Output> constructor() : Closeable {
113113
promptExecutor: PromptExecutor,
114114
agentConfig: AIAgentConfig,
115115
strategy: AIAgentGraphStrategy<String, String> = singleRunStrategy(),
116-
toolRegistry: ToolRegistry = ToolRegistry.EMPTY,
116+
toolRegistry: ToolRegistry = ToolRegistry.empty(),
117117
id: String? = null,
118118
installFeatures: FeatureContext.() -> Unit = {},
119119
): GraphAIAgent<String, String>
@@ -137,7 +137,7 @@ public expect abstract class AIAgent<Input, Output> constructor() : Closeable {
137137
promptExecutor: PromptExecutor,
138138
agentConfig: AIAgentConfig,
139139
strategy: AIAgentFunctionalStrategy<Input, Output>,
140-
toolRegistry: ToolRegistry = ToolRegistry.EMPTY,
140+
toolRegistry: ToolRegistry = ToolRegistry.empty(),
141141
id: String? = null,
142142
clock: Clock = Clock.System,
143143
installFeatures: FunctionalAIAgent.FeatureContext.() -> Unit = {},
@@ -165,7 +165,7 @@ public expect abstract class AIAgent<Input, Output> constructor() : Closeable {
165165
llmModel: LLModel,
166166
responseProcessor: ResponseProcessor? = null,
167167
strategy: AIAgentGraphStrategy<String, String> = singleRunStrategy(),
168-
toolRegistry: ToolRegistry = ToolRegistry.EMPTY,
168+
toolRegistry: ToolRegistry = ToolRegistry.empty(),
169169
id: String? = null,
170170
systemPrompt: String? = null,
171171
temperature: Double? = null,
@@ -199,7 +199,7 @@ public expect abstract class AIAgent<Input, Output> constructor() : Closeable {
199199
llmModel: LLModel,
200200
strategy: AIAgentGraphStrategy<Input, Output>,
201201
responseProcessor: ResponseProcessor? = null,
202-
toolRegistry: ToolRegistry = ToolRegistry.EMPTY,
202+
toolRegistry: ToolRegistry = ToolRegistry.empty(),
203203
id: String? = null,
204204
clock: Clock = Clock.System,
205205
systemPrompt: String? = null,
@@ -232,7 +232,7 @@ public expect abstract class AIAgent<Input, Output> constructor() : Closeable {
232232
promptExecutor: PromptExecutor,
233233
llmModel: LLModel,
234234
responseProcessor: ResponseProcessor? = null,
235-
toolRegistry: ToolRegistry = ToolRegistry.EMPTY,
235+
toolRegistry: ToolRegistry = ToolRegistry.empty(),
236236
strategy: AIAgentFunctionalStrategy<Input, Output>,
237237
id: String? = null,
238238
systemPrompt: String? = null,
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package ai.koog.agents.core.feature.handler.tool
2+
3+
import ai.koog.agents.core.annotation.InternalAgentsApi
4+
5+
/**
6+
* Comprehensive metadata describing an MCP server connection.
7+
*
8+
* This class captures both the server's identity and the active connection details,
9+
* including protocol version, transport type, and session information. This metadata
10+
* is essential for tracking server capabilities, managing connections, and debugging
11+
* tool execution issues.
12+
*
13+
* @property serverUrl The URL where the server is accessible, if applicable (e.g., "http://localhost").
14+
* @property serverPort The port number on which the server listens, if applicable.
15+
* @property siteUrl The server's public website URL, if provided by the server.
16+
* @property instructions Optional server-provided instructions or documentation for using its tools.
17+
* @property mcpProtocolVersion The MCP protocol version supported by this server connection.
18+
* @property mcpTransportType The transport protocol type used for this connection (e.g., pipe for stdio, tcp for HTTP).
19+
* TODO: support [sessionId] when it would be available in the sdk client
20+
* @property sessionId Unique identifier for the current MCP session, if session management is enabled.
21+
*/
22+
@InternalAgentsApi
23+
public class McpServerMeta(
24+
public val serverUrl: String?,
25+
public val serverPort: Int?,
26+
public val siteUrl: String?,
27+
public val instructions: String?,
28+
public val mcpProtocolVersion: String,
29+
public val mcpTransportType: McpTransportType,
30+
public val sessionId: String?,
31+
)
32+
33+
/**
34+
* Information about an MCP server instance.
35+
*/
36+
public class McpServerInfo(
37+
public val url: String? = null,
38+
public val command: String? = null,
39+
)
40+
41+
/**
42+
* Enumeration of supported MCP server transport protocol types.
43+
*
44+
* This enum defines the different communication mechanisms that can be used
45+
* to establish connections with MCP servers. Each transport type has distinct
46+
* characteristics and is suitable for different deployment scenarios.
47+
*
48+
* @property value The canonical name of the transport protocol.
49+
*/
50+
public enum class McpTransportType(public val value: String) {
51+
/**
52+
* Pipe-based transport using standard input/output streams.
53+
*
54+
* This transport is typically used for local MCP servers running as separate
55+
* processes that communicate via stdio. Suitable for development and local tools.
56+
*/
57+
Pipe("pipe"),
58+
59+
/**
60+
* TCP-based transport using HTTP or other network protocols.
61+
*
62+
* This transport enables communication with remote MCP servers over network
63+
* connections, including HTTP, SSE (Server-Sent Events), or custom TCP protocols.
64+
* Suitable for distributed architectures and cloud-based MCP servers.
65+
*/
66+
Tcp("tcp"),
67+
}

agents/agents-core/src/commonMain/kotlin/ai/koog/agents/core/feature/handler/tool/ToolCallEventContext.kt

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,54 @@ import kotlinx.serialization.json.JsonElement
99
import kotlinx.serialization.json.JsonObject
1010

1111
/**
12-
* Represents the context for handling tool-specific events within the framework.
12+
* Represents the context for handling tool call events.
1313
*/
14-
public interface ToolCallEventContext : AgentLifecycleEventContext
14+
public interface ToolCallEventContext : AgentLifecycleEventContext {
15+
/**
16+
* [runId] The unique identifier for this tool call session;
17+
*/
18+
public val runId: String
19+
20+
/**
21+
* [toolCallId] The unique identifier for this tool call;
22+
*/
23+
public val toolCallId: String?
24+
25+
/**
26+
* [toolName] The tool name that is being executed;
27+
*/
28+
public val toolName: String
29+
30+
/**
31+
* [toolDescription] A description of the tool being executed;
32+
*/
33+
public val toolDescription: String?
34+
35+
/**
36+
* [toolArgs] The arguments provided for the tool execution, adhering to the tool's expected input structure.
37+
*/
38+
public val toolArgs: JsonObject
39+
40+
/**
41+
* [context] The agent context associated with the tool call;
42+
*/
43+
public val context: AIAgentContext
44+
}
1545

1646
/**
1747
* Represents the context for handling a tool call event.
1848
*
1949
* @property executionInfo The execution information containing parentId and current execution path;
20-
* @property runId The unique identifier for this tool call session;
21-
* @property toolCallId The unique identifier for this tool call;
22-
* @property toolName The tool name that is being executed;
23-
* @property toolArgs The arguments provided for the tool execution, adhering to the tool's expected input structure.
2450
*/
2551
public data class ToolCallStartingContext(
2652
override val eventId: String,
2753
override val executionInfo: AgentExecutionInfo,
28-
val runId: String,
29-
val toolCallId: String?,
30-
val toolName: String,
31-
val toolDescription: String?,
32-
val toolArgs: JsonObject,
33-
val context: AIAgentContext
54+
override val runId: String,
55+
override val toolCallId: String?,
56+
override val toolName: String,
57+
override val toolDescription: String?,
58+
override val toolArgs: JsonObject,
59+
override val context: AIAgentContext
3460
) : ToolCallEventContext {
3561
override val eventType: AgentLifecycleEventType = AgentLifecycleEventType.ToolCallStarting
3662
}
@@ -39,25 +65,20 @@ public data class ToolCallStartingContext(
3965
* Represents the context for handling validation errors that occur during the execution of a tool.
4066
*
4167
* @property executionInfo The execution information containing parentId and current execution path;
42-
* @property runId The unique identifier for this tool call session;
43-
* @property toolCallId The unique identifier for this tool call;
44-
* @property toolName The name of the tool associated with the validation error;
45-
* @property toolDescription A description of the tool being executed;
46-
* @property toolArgs The arguments passed to the tool when the error occurred;
4768
* @property message A message describing the validation error.
4869
* @property error The [AIAgentError] error describing the validation issue.
4970
*/
5071
public data class ToolValidationFailedContext(
5172
override val eventId: String,
5273
override val executionInfo: AgentExecutionInfo,
53-
val runId: String,
54-
val toolCallId: String?,
55-
val toolName: String,
56-
val toolDescription: String?,
57-
val toolArgs: JsonObject,
74+
override val runId: String,
75+
override val toolCallId: String?,
76+
override val toolName: String,
77+
override val toolDescription: String?,
78+
override val toolArgs: JsonObject,
5879
val message: String,
5980
val error: AIAgentError,
60-
val context: AIAgentContext
81+
override val context: AIAgentContext
6182
) : ToolCallEventContext {
6283
override val eventType: AgentLifecycleEventType = AgentLifecycleEventType.ToolValidationFailed
6384
}
@@ -66,25 +87,20 @@ public data class ToolValidationFailedContext(
6687
* Represents the context provided to handle a failure during the execution of a tool.
6788
*
6889
* @property executionInfo The execution information containing parentId and current execution path;
69-
* @property runId The unique identifier for this tool call session;
70-
* @property toolCallId The unique identifier for this tool call;
71-
* @property toolName The name of the tool being executed when the failure occurred;
72-
* @property toolDescription A description of the tool being executed;
73-
* @property toolArgs The arguments that were passed to the tool during execution;
7490
* @property message A message describing the failure that occurred.
7591
* @property error The [AIAgentError] instance describing the tool call failure.
7692
*/
7793
public data class ToolCallFailedContext(
7894
override val eventId: String,
7995
override val executionInfo: AgentExecutionInfo,
80-
val runId: String,
81-
val toolCallId: String?,
82-
val toolName: String,
83-
val toolDescription: String?,
84-
val toolArgs: JsonObject,
96+
override val runId: String,
97+
override val toolCallId: String?,
98+
override val toolName: String,
99+
override val toolDescription: String?,
100+
override val toolArgs: JsonObject,
85101
val message: String,
86102
val error: AIAgentError?,
87-
val context: AIAgentContext
103+
override val context: AIAgentContext
88104
) : ToolCallEventContext {
89105
override val eventType: AgentLifecycleEventType = AgentLifecycleEventType.ToolCallFailed
90106
}
@@ -93,23 +109,18 @@ public data class ToolCallFailedContext(
93109
* Represents the context used when handling the result of a tool call.
94110
*
95111
* @property executionInfo The execution information containing parentId and current execution path;
96-
* @property runId The unique identifier for this tool call session;
97-
* @property toolCallId The unique identifier for this tool call;
98-
* @property toolName The name of the tool being executed;
99-
* @property toolDescription A description of the tool being executed;
100-
* @property toolArgs The arguments required by the tool for execution;
101112
* @property toolResult An optional result produced by the tool after execution can be null if not applicable.
102113
*/
103114
public data class ToolCallCompletedContext(
104115
override val eventId: String,
105116
override val executionInfo: AgentExecutionInfo,
106-
val runId: String,
107-
val toolCallId: String?,
108-
val toolName: String,
109-
val toolDescription: String?,
110-
val toolArgs: JsonObject,
117+
override val runId: String,
118+
override val toolCallId: String?,
119+
override val toolName: String,
120+
override val toolDescription: String?,
121+
override val toolArgs: JsonObject,
111122
val toolResult: JsonElement?,
112-
val context: AIAgentContext
123+
override val context: AIAgentContext
113124
) : ToolCallEventContext {
114125
override val eventType: AgentLifecycleEventType = AgentLifecycleEventType.ToolCallCompleted
115126
}

agents/agents-features/agents-features-opentelemetry/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ kotlin {
4343
dependencies {
4444
implementation(kotlin("test-junit5"))
4545
implementation(project(":agents:agents-test"))
46+
implementation(libs.opentelemetry.sdk.testing)
4647
}
4748
}
4849
}

0 commit comments

Comments
 (0)