|
| 1 | +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import argparse |
| 17 | +from agent import agent, app_name, short_term_memory |
| 18 | +from veadk.tracing.base_tracer import BaseTracer |
| 19 | +from veadk.tracing.telemetry.opentelemetry_tracer import OpentelemetryTracer |
| 20 | +from veadk import Agent |
| 21 | +from veadk.memory.short_term_memory import ShortTermMemory |
| 22 | +from veadk.runner import Runner |
| 23 | +from fastmcp import FastMCP |
| 24 | + |
| 25 | + |
| 26 | +# ============================================================================== |
| 27 | +# Tracer Config ================================================================ |
| 28 | + |
| 29 | +TRACERS: list[BaseTracer] = [] |
| 30 | + |
| 31 | +exporters = [] |
| 32 | +if os.getenv("VEADK_TRACER_APMPLUS", "").lower() == "true": |
| 33 | + from veadk.tracing.telemetry.exporters.apmplus_exporter import APMPlusExporter |
| 34 | + |
| 35 | + exporters.append(APMPlusExporter()) |
| 36 | + |
| 37 | +if os.getenv("VEADK_TRACER_COZELOOP", "").lower() == "true": |
| 38 | + from veadk.tracing.telemetry.exporters.cozeloop_exporter import CozeloopExporter |
| 39 | + |
| 40 | + exporters.append(CozeloopExporter()) |
| 41 | + |
| 42 | +if os.getenv("VEADK_TRACER_TLS", "").lower() == "true": |
| 43 | + from veadk.tracing.telemetry.exporters.tls_exporter import TLSExporter |
| 44 | + |
| 45 | + exporters.append(TLSExporter()) |
| 46 | + |
| 47 | +TRACERS.append(OpentelemetryTracer(exporters=exporters)) |
| 48 | + |
| 49 | + |
| 50 | +agent.tracers.extend(TRACERS) |
| 51 | +if not getattr(agent, "before_model_callback", None): |
| 52 | + agent.before_model_callback = [] |
| 53 | +if not getattr(agent, "after_model_callback", None): |
| 54 | + agent.after_model_callback = [] |
| 55 | +if not getattr(agent, "after_tool_callback", None): |
| 56 | + agent.after_tool_callback = [] |
| 57 | +for tracer in TRACERS: |
| 58 | + if tracer.tracer_hook_before_model not in agent.before_model_callback: |
| 59 | + agent.before_model_callback.append(tracer.tracer_hook_before_model) |
| 60 | + if tracer.tracer_hook_after_model not in agent.after_model_callback: |
| 61 | + agent.after_model_callback.append(tracer.tracer_hook_after_model) |
| 62 | + if tracer.tracer_hook_after_tool not in agent.after_tool_callback: |
| 63 | + agent.after_tool_callback.append(tracer.tracer_hook_after_tool) |
| 64 | + |
| 65 | +# Tracer Config ================================================================ |
| 66 | +# ============================================================================== |
| 67 | + |
| 68 | + |
| 69 | +class VeMCPServer: |
| 70 | + def __init__(self, agent: Agent, app_name: str, short_term_memory: ShortTermMemory): |
| 71 | + self.agent = agent |
| 72 | + self.app_name = app_name |
| 73 | + self.short_term_memory = short_term_memory |
| 74 | + |
| 75 | + self.runner = Runner( |
| 76 | + agent=self.agent, |
| 77 | + short_term_memory=self.short_term_memory, |
| 78 | + app_name=app_name, |
| 79 | + user_id="", # waiting for tool call to provide user_id |
| 80 | + ) |
| 81 | + |
| 82 | + def build(self) -> FastMCP: |
| 83 | + # Create MCP server |
| 84 | + mcp = FastMCP(name=self.app_name) |
| 85 | + |
| 86 | + @mcp.tool |
| 87 | + async def run_agent( |
| 88 | + user_input: str, |
| 89 | + user_id: str = "unknown_user", |
| 90 | + session_id: str = "unknown_session", |
| 91 | + ) -> str: |
| 92 | + """ |
| 93 | + Execute agent with user input and return final output |
| 94 | + Args: |
| 95 | + user_input: str, user_id: str = "unknown_user", session_id: str = "unknown_session" |
| 96 | + Returns: |
| 97 | + final_output: str |
| 98 | + """ |
| 99 | + # Set user_id for runner |
| 100 | + self.runner.user_id = user_id |
| 101 | + |
| 102 | + # Running agent and get final output |
| 103 | + final_output = await self.runner.run( |
| 104 | + messages=user_input, |
| 105 | + session_id=session_id, |
| 106 | + ) |
| 107 | + |
| 108 | + return final_output |
| 109 | + |
| 110 | + return mcp |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + parser = argparse.ArgumentParser(description="MCP Server") |
| 115 | + parser.add_argument( |
| 116 | + "--transport", default="http", help="Transport type (default: http)" |
| 117 | + ) |
| 118 | + parser.add_argument( |
| 119 | + "--host", default="0.0.0.0", help="Host address (default: 0.0.0.0)" |
| 120 | + ) |
| 121 | + parser.add_argument( |
| 122 | + "--port", type=int, default=8000, help="Port number (default: 8000)" |
| 123 | + ) |
| 124 | + parser.add_argument("--log-level", default="INFO", help="Log level (default: INFO)") |
| 125 | + |
| 126 | + args = parser.parse_args() |
| 127 | + |
| 128 | + server = VeMCPServer( |
| 129 | + agent=agent, |
| 130 | + app_name=app_name, |
| 131 | + short_term_memory=short_term_memory, |
| 132 | + ) |
| 133 | + mcp = server.build() |
| 134 | + mcp.run(transport=args.transport, host=args.host, port=args.port) |
0 commit comments