The free tier allows 1,000 API calls per day, which is plenty for development.
2
Create the weather tool
Create a new file called weather_tools.py in your project directory:
weather_tools.py
import osimport requestsfrom typing import Optionaldef get_weather( city: str, country_code: Optional[str] = None, units: str = "metric") -> dict: """ Fetches current weather information for a specified city. Args: city: The name of the city (e.g., "London", "New York") country_code: Optional ISO 3166 country code (e.g., "US", "GB") units: Temperature units - "metric" (Celsius), "imperial" (Fahrenheit), or "standard" (Kelvin) Returns: A dictionary containing weather information including: - temperature: Current temperature - feels_like: Feels like temperature - description: Weather description - humidity: Humidity percentage - wind_speed: Wind speed - city: City name - country: Country code """ api_key = os.getenv("OPENWEATHER_API_KEY") if not api_key: return { "status": "error", "message": "OPENWEATHER_API_KEY not found in environment variables" } # Build location query location = city if country_code: location = f"{city},{country_code}" # Construct API request base_url = "https://api.openweathermap.org/data/2.5/weather" params = { "q": location, "appid": api_key, "units": units } try: response = requests.get(base_url, params=params, timeout=10) response.raise_for_status() data = response.json() # Extract relevant information weather_info = { "status": "success", "city": data["name"], "country": data["sys"]["country"], "temperature": data["main"]["temp"], "feels_like": data["main"]["feels_like"], "description": data["weather"][0]["description"], "humidity": data["main"]["humidity"], "wind_speed": data["wind"]["speed"], "units": units } return weather_info except requests.exceptions.HTTPError as e: if e.response.status_code == 404: return { "status": "error", "message": f"City '{city}' not found. Please check the spelling." } return { "status": "error", "message": f"API error: {str(e)}" } except requests.exceptions.RequestException as e: return { "status": "error", "message": f"Network error: {str(e)}" } except Exception as e: return { "status": "error", "message": f"Unexpected error: {str(e)}" }
Install the required dependency:
pip install requests
3
Create the weather agent configuration
Create weather_agent.yaml:
weather_agent.yaml
log: stdout_log_level: INFO log_file_level: DEBUG log_file: weather_agent.log!include shared_config.yamlapps: - name: weather_agent_app app_base_path: . app_module: solace_agent_mesh.agent.sac.app broker: <<: *broker_connection app_config: namespace: ${NAMESPACE} agent_name: "WeatherAgent" display_name: "Weather Information Agent" model: *planning_model instruction: | You are a weather information agent. You can provide current weather information for any city in the world. When a user asks about weather: 1. Use the get_weather tool to fetch current conditions 2. Present the information in a friendly, conversational way 3. Include temperature, conditions, humidity, and wind speed 4. Suggest appropriate clothing or activities based on conditions If the city is not found, politely ask the user to check the spelling or provide the country code for disambiguation. # Register the Python tool tools: - tool_type: python component_module: weather_tools component_base_path: . function_name: get_weather tool_name: "get_weather" supports_streaming: true session_service: type: "memory" default_behavior: "PERSISTENT" artifact_service: type: "filesystem" base_path: "/tmp/samv2" artifact_scope: namespace agent_card: description: | Provides real-time weather information for cities worldwide. Can fetch current temperature, conditions, humidity, and wind data. defaultInputModes: ["text"] defaultOutputModes: ["text"] skills: - id: "get_weather" name: "Get Weather Information" description: "Fetches current weather data for any city" examples: - "What's the weather in London?" - "Tell me the temperature in Tokyo" - "How's the weather in New York, US?" tags: ["weather", "api", "external-data"] agent_card_publishing: { interval_seconds: 10 } agent_discovery: { enabled: true }
4
Run and test the weather agent
Start your agent:
sam run
Open the Web UI at http://localhost:8000 and try these queries:Test 1: Basic weather query
What's the weather in Paris?
Expected response:
The current weather in Paris, France:- Temperature: 15°C (feels like 13°C)- Conditions: partly cloudy- Humidity: 72%- Wind speed: 3.5 m/sIt's a bit cool and cloudy. I'd recommend bringing a light jacket!
In your agent configuration, you register the Python tool:
tools: - tool_type: python component_module: weather_tools # Python module name component_base_path: . # Path to the module function_name: get_weather # Function to call tool_name: "get_weather" # Name the LLM uses
The LLM automatically understands your tool from the function signature and docstring:
def get_weather( city: str, # Required parameter country_code: Optional[str] = None, # Optional parameter units: str = "metric" # Parameter with default) -> dict: """ Fetches current weather information... Args: city: The name of the city # Parameter descriptions country_code: Optional ISO 3166 country code units: Temperature units... Returns: A dictionary containing... # Return value description """
Always use type hints and comprehensive docstrings. The LLM uses these to understand when and how to call your tool.
def get_forecast(city: str, days: int = 5) -> dict: """Get weather forecast for the next N days.""" # Use OpenWeatherMap forecast API base_url = "https://api.openweathermap.org/data/2.5/forecast" # ... implementation
Add air quality data
Extend with air quality information:
def get_air_quality(city: str) -> dict: """Get current air quality index for a city.""" # Use OpenWeatherMap air pollution API base_url = "https://api.openweathermap.org/data/2.5/air_pollution" # ... implementation
Your weather agent can work with other agents. For example, create a travel advisor agent:
instruction: | You are a travel advisor. Use the WeatherAgent to check weather conditions when users ask about travel plans. When suggesting destinations or packing lists, always check the current weather first.inter_agent_communication: allow_list: ["WeatherAgent"]
Now users can ask: “I’m planning a trip to Barcelona next week. What should I pack?”The orchestrator will:
Route the query to the travel advisor
The advisor calls WeatherAgent for current conditions
The advisor provides packing suggestions based on weather
Always test tools independently before integrating with agents.
Create a test file test_weather_tools.py:
test_weather_tools.py
import osfrom weather_tools import get_weather# Set your API keyos.environ["OPENWEATHER_API_KEY"] = "your_key_here"def test_valid_city(): result = get_weather("London", "GB") assert result["status"] == "success" assert "temperature" in result print("✓ Valid city test passed")def test_invalid_city(): result = get_weather("InvalidCityName123") assert result["status"] == "error" print("✓ Invalid city test passed")def test_units(): # Test metric units metric = get_weather("Paris", units="metric") imperial = get_weather("Paris", units="imperial") assert metric["temperature"] != imperial["temperature"] print("✓ Units conversion test passed")if __name__ == "__main__": test_valid_city() test_invalid_city() test_units() print("\nAll tests passed!")
You now know how to create agents that can interact with external services. This is a fundamental pattern you’ll use throughout your agent mesh development!