11package ai.koog.agents.example.features.langfuse
22
33import ai.koog.agents.core.agent.AIAgent
4+ import ai.koog.agents.core.tools.ToolRegistry
5+ import ai.koog.agents.core.tools.annotations.LLMDescription
6+ import ai.koog.agents.core.tools.annotations.Tool
7+ import ai.koog.agents.core.tools.reflect.tool
48import ai.koog.agents.example.ApiKeyService
59import ai.koog.agents.features.opentelemetry.feature.OpenTelemetry
610import ai.koog.agents.features.opentelemetry.integration.langfuse.addLangfuseExporter
11+ import ai.koog.agents.mcp.McpToolRegistryProvider
12+ import ai.koog.agents.mcp.server.startSseMcpServer
713import ai.koog.prompt.executor.clients.openai.OpenAIModels
814import ai.koog.prompt.executor.llms.all.simpleOpenAIExecutor
15+ import io.ktor.server.cio.*
916import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter
17+ import kotlinx.coroutines.delay
18+ import kotlin.time.Duration.Companion.seconds
1019
1120/* *
1221 * Example of Koog agents tracing to [Langfuse](https://langfuse.com/)
@@ -18,15 +27,22 @@ import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter
1827 * 1. Set up a Langfuse project and credentials as described [here](https://langfuse.com/docs/get-started#create-new-project-in-langfuse)
1928 * 2. Get Langfuse credentials as described [here](https://langfuse.com/faq/all/where-are-langfuse-api-keys)
2029 * 3. Set `LANGFUSE_HOST`, `LANGFUSE_PUBLIC_KEY`, and `LANGFUSE_SECRET_KEY` environment variables
30+ * 4. Set `OPENAI_API_KEY` from [here](https://platform.openai.com/account/api-keys)
2131 *
2232 * @see <a href="https://langfuse.com/docs/opentelemetry/get-started#opentelemetry-endpoint">Langfuse OpenTelemetry Docs</a>
2333 */
2434suspend fun main () {
25- simpleOpenAIExecutor(ApiKeyService .openAIApiKey).use { executor ->
35+ val server = startSseMcpServer(CIO , port = 30001 , host = " localhost" , ToolRegistry {
36+ tool(::applyArithmetic)
37+ })
38+ delay(1 .seconds)
39+ try {
40+ val tools = McpToolRegistryProvider .fromSseUrl(" http://localhost:30001" )
2641 val agent = AIAgent (
27- promptExecutor = executor,
28- llmModel = OpenAIModels .Chat .GPT4oMini ,
29- systemPrompt = " You are a code assistant. Provide concise code examples."
42+ promptExecutor = simpleOpenAIExecutor(ApiKeyService .openAIApiKey),
43+ llmModel = OpenAIModels .Chat .GPT4_1Mini ,
44+ systemPrompt = " You are a code assistant. Provide concise code examples." ,
45+ toolRegistry = ToolRegistry { tool(::sin); tool(::cos) } + tools
3046 ) {
3147 install(OpenTelemetry ) {
3248 addLangfuseExporter()
@@ -35,8 +51,36 @@ suspend fun main() {
3551
3652 println (" Running agent with Langfuse tracing" )
3753
38- val result = agent.run (" Tell me a joke about programming " )
54+ val result = agent.run (" Compute (sin(1) + cos(2)) * 69 using available tools " )
3955
4056 println (" Result: $result \n See traces on the Langfuse instance" )
57+ } finally {
58+ server.close()
4159 }
4260}
61+
62+ @Tool
63+ @LLMDescription(" Calculates the result of an arithmetic expression" )
64+ fun applyArithmetic (
65+ @LLMDescription(" Arithmetic operation: +-*/" ) op : String ,
66+ @LLMDescription(" First operand" ) a : Double ,
67+ @LLMDescription(" Second operand" ) b : Double
68+ ): Double = when (op) {
69+ " +" -> a + b
70+ " -" -> a - b
71+ " *" -> a * b
72+ " /" -> a / b
73+ else -> throw IllegalArgumentException (" Unsupported operation: $op " )
74+ }
75+
76+ @Tool
77+ @LLMDescription(" sin" )
78+ fun sin (
79+ @LLMDescription(" Operand" ) x : Double
80+ ): Double = kotlin.math.sin(x)
81+
82+ @Tool
83+ @LLMDescription(" cos" )
84+ fun cos (
85+ @LLMDescription(" Operand" ) x : Double
86+ ): Double = kotlin.math.cos(x)
0 commit comments