[ORGANIZE MCP EXAMPLES]

pull/1054/head
Kye Gomez 4 days ago
parent 30b94c0d6b
commit 438029dbe5

@ -27,7 +27,9 @@ def create_sample_agent():
# Example 1: Simple output transformation callback
def transform_output_callback(output: Any, task: str, metadata: Dict) -> Dict:
def transform_output_callback(
output: Any, task: str, metadata: Dict
) -> Dict:
"""Transform the agent output into a structured format.
Args:
@ -40,16 +42,24 @@ def transform_output_callback(output: Any, task: str, metadata: Dict) -> Dict:
"""
return {
"original_output": output,
"transformed_at": datetime.fromtimestamp(metadata["timestamp"]).isoformat(),
"transformed_at": datetime.fromtimestamp(
metadata["timestamp"]
).isoformat(),
"execution_number": metadata["execution_count"],
"task_executed": task,
"job_status": "running" if metadata["is_running"] else "stopped",
"uptime_seconds": metadata["uptime"] if metadata["start_time"] else 0
"job_status": (
"running" if metadata["is_running"] else "stopped"
),
"uptime_seconds": (
metadata["uptime"] if metadata["start_time"] else 0
),
}
# Example 2: Output filtering and enhancement callback
def filter_and_enhance_callback(output: Any, task: str, metadata: Dict) -> Dict:
def filter_and_enhance_callback(
output: Any, task: str, metadata: Dict
) -> Dict:
"""Filter and enhance the output based on execution count and content.
Args:
@ -62,13 +72,21 @@ def filter_and_enhance_callback(output: Any, task: str, metadata: Dict) -> Dict:
"""
# Only include outputs that contain certain keywords
if isinstance(output, str):
if any(keyword in output.lower() for keyword in ["important", "key", "significant", "trend"]):
if any(
keyword in output.lower()
for keyword in [
"important",
"key",
"significant",
"trend",
]
):
enhanced_output = {
"content": output,
"priority": "high",
"execution_id": metadata["execution_count"],
"timestamp": metadata["timestamp"],
"analysis_type": "priority_content"
"analysis_type": "priority_content",
}
else:
enhanced_output = {
@ -76,7 +94,7 @@ def filter_and_enhance_callback(output: Any, task: str, metadata: Dict) -> Dict:
"priority": "normal",
"execution_id": metadata["execution_count"],
"timestamp": metadata["timestamp"],
"analysis_type": "standard_content"
"analysis_type": "standard_content",
}
else:
enhanced_output = {
@ -84,7 +102,7 @@ def filter_and_enhance_callback(output: Any, task: str, metadata: Dict) -> Dict:
"priority": "unknown",
"execution_id": metadata["execution_count"],
"timestamp": metadata["timestamp"],
"analysis_type": "non_string_content"
"analysis_type": "non_string_content",
}
return enhanced_output
@ -100,7 +118,9 @@ class MonitoringCallback:
self.success_count = 0
self.last_execution_time = None
def __call__(self, output: Any, task: str, metadata: Dict) -> Dict:
def __call__(
self, output: Any, task: str, metadata: Dict
) -> Dict:
"""Monitor and track execution metrics.
Args:
@ -138,11 +158,13 @@ class MonitoringCallback:
"timestamp": metadata["timestamp"],
"task": task,
"metrics": {
"success_rate": self.success_count / (self.success_count + self.error_count),
"total_executions": self.success_count + self.error_count,
"success_rate": self.success_count
/ (self.success_count + self.error_count),
"total_executions": self.success_count
+ self.error_count,
"error_count": self.error_count,
"success_count": self.success_count
}
"success_count": self.success_count,
},
}
self.output_history.append(monitoring_data)
@ -157,14 +179,21 @@ class MonitoringCallback:
"total_executions": self.success_count + self.error_count,
"success_count": self.success_count,
"error_count": self.error_count,
"success_rate": self.success_count / (self.success_count + self.error_count) if (self.success_count + self.error_count) > 0 else 0,
"success_rate": (
self.success_count
/ (self.success_count + self.error_count)
if (self.success_count + self.error_count) > 0
else 0
),
"history_length": len(self.output_history),
"last_execution_time": self.last_execution_time
"last_execution_time": self.last_execution_time,
}
# Example 4: API integration callback
def api_webhook_callback(output: Any, task: str, metadata: Dict) -> Dict:
def api_webhook_callback(
output: Any, task: str, metadata: Dict
) -> Dict:
"""Callback that could send output to an external API.
Args:
@ -182,17 +211,19 @@ def api_webhook_callback(output: Any, task: str, metadata: Dict) -> Dict:
"job_id": metadata["job_id"],
"execution_id": metadata["execution_count"],
"timestamp": metadata["timestamp"],
"task": task
"task": task,
}
# Simulate API call (replace with actual HTTP request)
logger.info(f"Would send to API: {json.dumps(api_payload, indent=2)}")
logger.info(
f"Would send to API: {json.dumps(api_payload, indent=2)}"
)
return {
"output": output,
"api_status": "sent",
"api_payload": api_payload,
"execution_id": metadata["execution_count"]
"execution_id": metadata["execution_count"],
}
@ -209,7 +240,7 @@ def main():
agent=agent,
interval="15seconds",
job_id="transform-example",
callback=transform_output_callback
callback=transform_output_callback,
)
# Example 2: Filtering and enhancement callback
@ -218,7 +249,7 @@ def main():
agent=agent,
interval="20seconds",
job_id="filter-example",
callback=filter_and_enhance_callback
callback=filter_and_enhance_callback,
)
# Example 3: Monitoring callback
@ -228,7 +259,7 @@ def main():
agent=agent,
interval="25seconds",
job_id="monitoring-example",
callback=monitoring_callback
callback=monitoring_callback,
)
# Example 4: API integration callback
@ -237,7 +268,7 @@ def main():
agent=agent,
interval="30seconds",
job_id="api-example",
callback=api_webhook_callback
callback=api_webhook_callback,
)
# Start all cron jobs
@ -258,20 +289,22 @@ def main():
"Analyze the current market trends and provide key insights",
"What are the most important factors affecting today's economy?",
"Provide a summary of recent technological developments",
"Analyze the impact of current events on business operations"
"Analyze the impact of current events on business operations",
]
for i, (cron_job, task) in enumerate([
for i, (cron_job, task) in enumerate(
[
(transform_cron, tasks[0]),
(filter_cron, tasks[1]),
(monitoring_cron, tasks[2]),
(api_cron, tasks[3])
]):
(api_cron, tasks[3]),
]
):
thread = threading.Thread(
target=run_cron,
args=(cron_job, task),
daemon=True,
name=f"cron-thread-{i}"
name=f"cron-thread-{i}",
)
thread.start()
threads.append(thread)
@ -285,28 +318,39 @@ def main():
# Show monitoring summary
logger.info("📈 Monitoring Summary:")
logger.info(json.dumps(monitoring_callback.get_summary(), indent=2))
logger.info(
json.dumps(monitoring_callback.get_summary(), indent=2)
)
# Show execution stats for each cron job
for cron_job, name in [
(transform_cron, "Transform"),
(filter_cron, "Filter"),
(monitoring_cron, "Monitoring"),
(api_cron, "API")
(api_cron, "API"),
]:
stats = cron_job.get_execution_stats()
logger.info(f"{name} Cron Stats: {json.dumps(stats, indent=2)}")
logger.info(
f"{name} Cron Stats: {json.dumps(stats, indent=2)}"
)
except KeyboardInterrupt:
logger.info("⏹️ Stopping all cron jobs...")
# Stop all cron jobs
for cron_job in [transform_cron, filter_cron, monitoring_cron, api_cron]:
for cron_job in [
transform_cron,
filter_cron,
monitoring_cron,
api_cron,
]:
cron_job.stop()
# Show final monitoring summary
logger.info("📊 Final Monitoring Summary:")
logger.info(json.dumps(monitoring_callback.get_summary(), indent=2))
logger.info(
json.dumps(monitoring_callback.get_summary(), indent=2)
)
if __name__ == "__main__":

@ -6,7 +6,6 @@ in CronJob to customize output while the job is running.
"""
import json
import time
from datetime import datetime
from loguru import logger
@ -38,9 +37,11 @@ def simple_callback(output, task, metadata):
return {
"agent_output": output,
"execution_number": metadata["execution_count"],
"timestamp": datetime.fromtimestamp(metadata["timestamp"]).isoformat(),
"timestamp": datetime.fromtimestamp(
metadata["timestamp"]
).isoformat(),
"task": task,
"job_id": metadata["job_id"]
"job_id": metadata["job_id"],
}
@ -55,11 +56,13 @@ def main():
agent=agent,
interval="10seconds",
job_id="simple-callback-example",
callback=simple_callback
callback=simple_callback,
)
logger.info("▶️ Starting cron job with callback...")
logger.info("📝 The callback will enhance each output with metadata")
logger.info(
"📝 The callback will enhance each output with metadata"
)
logger.info("⏹️ Press Ctrl+C to stop")
try:

@ -1,4 +1,3 @@
# crypto_price_server.py
from mcp.server.fastmcp import FastMCP
import requests
@ -113,4 +112,4 @@ def get_htx_crypto_price(symbol: str) -> str:
if __name__ == "__main__":
mcp.run(transport="sse")
mcp.run()

@ -42,6 +42,7 @@ def get_reward(input: str) -> int:
else:
return 0
def get_agent_judge_prompt() -> str:
"""
Returns the main system prompt for the agent judge.

@ -278,17 +278,23 @@ class CronJob:
"task": task,
"kwargs": kwargs,
"start_time": self.start_time,
"is_running": self.is_running
"is_running": self.is_running,
}
# Apply callback if provided
if self.callback:
try:
customized_output = self.callback(original_output, task, metadata)
logger.debug(f"Callback applied to job {self.job_id}, execution {self.execution_count}")
customized_output = self.callback(
original_output, task, metadata
)
logger.debug(
f"Callback applied to job {self.job_id}, execution {self.execution_count}"
)
return customized_output
except Exception as callback_error:
logger.warning(f"Callback failed for job {self.job_id}: {callback_error}")
logger.warning(
f"Callback failed for job {self.job_id}: {callback_error}"
)
# Return original output if callback fails
return original_output
@ -425,8 +431,12 @@ class CronJob:
"is_running": self.is_running,
"execution_count": self.execution_count,
"start_time": self.start_time,
"uptime": time.time() - self.start_time if self.start_time else 0,
"interval": self.interval
"uptime": (
time.time() - self.start_time
if self.start_time
else 0
),
"interval": self.interval,
}

Loading…
Cancel
Save