Skip to main content
Glama
dbalencar

Weather MCP Server

by dbalencar
mcp_client.cpython-313.pyc11.1 kB
� ߂h9�� �SrSSKrSSKrSSKrSSKrSSKJrJrJr SSK r SSK J r \ "5 \R"\RS9 \R"\5r"SS5r"SS 5rS r\S :Xa\R("\"55 gg) z: MCP Client for communicating with the Weather MCP Server �N)�Any�Dict�Optional)� load_dotenv)�levelc ���\rSrSrSrSS\4SjjrSrSrSS\S \ \\ 4S \ \\ 44S jjr S \ \\ 44S jr SS \S\ \S \ \\ 44SjjrSS \S\ \S\S \ \\ 44SjjrSrg)� MCPClient�z-MCP Client for communicating with MCP serversN� server_urlc��U=(d2 S[R"SS5S[R"SS53UlSUlg)Nzhttp://�MCP_SERVER_HOST� localhost�:�MCP_SERVER_PORT�8000)�os�getenvr �session��selfr s �9/Users/maccabee/workspace/ai/mcp/python-sdk/mcp_client.py�__init__�MCPClient.__init__sS��$�E�'�"�)�)�<M�{�2[�1\�\]�^`�^g�^g�hy�|B�_C�^D�)E����� �c��B# �[R"5UlU$7f�N)�aiohttp� ClientSessionr)rs r� __aenter__�MCPClient.__aenter__s����,�,�.�� �� �s�c��x# �UR(a#URR5IShv�N ggN7fr)r�close)r�exc_type�exc_val�exc_tbs r� __aexit__�MCPClient.__aexit__ s*��� �<�<��,�,�$�$�&� &� &� � &�s �/:�8�:�method�params�returnc��|# �UR(d [S5eUU=(d 0S.nURRURS3US9IShv�N nUR 5 UR 5IShv�N nUsSSS5IShv�N $N?NN !,IShv�N (df  g=f![ Ra2n[RSU35 SS[U530sSnA$SnAf[a2n[RS U35 SS [U530sSnA$SnAff=f7f) z Make a request to the MCP serverz2Client not initialized. Use async context manager.)r(r)z/mcp)�jsonNz$Error making request to MCP server: �errorz!Failed to connect to MCP server: zUnexpected error: ) r� RuntimeError�postr �raise_for_statusr,r� ClientError�loggerr-�str� Exception)rr(r)� request_data�response�result�es r� _make_request�MCPClient._make_request$s ����|�|��S�T� T���l�� � � <��|�|�(�(�D�O�O�+<�D�)A� �(�U�U�Ya��)�)�+�'�}�}��.���V�U�U�.�V�U�U�U���"�"� K� �L�L�?��s�C� D��@��Q���I�J� J��� <� �L�L�-�a�S�1� 2��1�#�a�&��:�;� ;�� <�s��+D<�+B9�B�B9�$B�B�B� B9�B�B9�D<�B9�B�B9�B6�%B( �& B6�2B9�5D<�6B9�9D9� 'C:�4D9�5D<�: D9�'D4�.D9�/D<�4D9�9D<c��# �URRURS35IShv�N nUR5 UR 5IShv�N sSSS5IShv�N $N=NN !,IShv�N (df  g=f![ R a2n[RSU35 SS[U530sSnA$SnAff=f7f)zGet server capabilitiesz /capabilitiesNzError getting capabilities: r-zFailed to get capabilities: ) r�getr r0r,rr1r2r-r3)rr6r8s r�get_capabilities�MCPClient.get_capabilities:s���� F��|�|�'�'�4�?�?�*;�=�(I�J�J�h��)�)�+�%�]�]�_�,�K�J�J�,�K�J�J�J���"�"� F� �L�L�7��s�;� <��;�C��F�8�D�E� E�� F�s��C�,B�A.�B�$A4�A0�A4� B�(A2�)B�-C�.B�0A4�2B�4B �:A= �; B �B� C� B�C�"'C� C� C�C�C�city� country_codec��`# �SU0nU(aX#S'URSU5IShv�N $N7f)zGet current weather for a cityr?r@zweather/get_currentN�r9)rr?r@r)s r�get_current_weather�MCPClient.get_current_weatherDs3����$��� �%1�>� "��'�'�(=�v�F�F�F�F�� �%.�,�.�daysc��`# �XS.nU(aX$S'URSU5IShv�N $N7f)zGet weather forecast for a city)r?rFr@zweather/get_forecastNrB)rr?r@rFr)s r�get_weather_forecast�MCPClient.get_weather_forecastLs2����-�� �%1�>� "��'�'�(>��G�G�G�G�rE)r rr)N�)�__name__� __module__� __qualname__�__firstlineno__�__doc__r3rrr&rrr9r=rrC�intrH�__static_attributes__�rrr r s���7��3���'�<�#�<�t�C��H�~�<�QU�VY�[^�V^�Q_�<�,F��S�#�X��F�G�c�G��#��G�Z^�_b�dg�_g�Zh�G�H�s�H�(�3�-�H�^a�H�jn�or�tw�ow�jx�H�Hrr c �n�\rSrSrSrS S\4SjjrS S\S\\S\4S jjrSS\S\\S \ S\4S jjr S r g)�WeatherService�Tz+High-level weather service using MCP clientNr c��Xlgr�r rs rr�WeatherService.__init__Ws��$�rr?r@r*c��"# �[UR5IShv�N nURX5IShv�N nSU;aSUS3sSSS5IShv�N $URS05nSU;aSUS3sSSS5IShv�N $USSnUSSnUS nUS n US S n S USSUSSUSUSUSU SU S3n U R 5sSSS5IShv�N $N�N�N�NlN !,IShv�N (df  g=f7f)z,Get formatted weather information for a cityNr-zError getting weather: r7zWeather service error: � temperature�current� feels_like� description�humidity�wind�speedz Current weather in r?�, �countryu: • Temperature: u°C (feels like u°C) • Conditions: u • Humidity: u% • Wind Speed: z m/s )r r rCr<�strip) rr?r@�clientr7� weather_data�tempr\r]r^� wind_speed� weather_infos r�get_weather_info�WeatherService.get_weather_infoZsE����T�_�_�-�-��!�5�5�d�I�I�F��&� �0����0A�B� .�-�-� "�:�:�h��3�L��,�&�0��g�1F�0G�H�.�-�-� � �.�y�9�D�%�m�4�\�B�J�&�}�5�K�#�J�/�H�%�f�-�g�6�J� � ��(�)��L��,C�+D�E���'� �|�4�� ���j��� �� �L� �%�%�'�1.�-�-�I�.�-�-�-�s��D�C+�D�C5�C-�C5� D�C/�D�C5�8 D�C1�D� AC5� D�%C3�&D�-C5�/D�1D�3D�5D �;C> �< D �DrFc ���# �[UR5IShv�N nURXU5IShv�N nSU;aSUS3sSSS5IShv�N $URS05nSU;aSUS3sSSS5IShv�N $SUSSUS S 3n0nUS H5n U S R 5S n X�;a/X�'X�R U 5 M7 [ UR55SUH�up�U V s/sH o�SSPM n n [U 5[U 5- n[U 5n[U 5nU V s/sHo�SPM nn [[U5URS9nUSU S3- nUSUSSUSSUSS3- nUSUS3- nM� UR5sSSS5IShv�N $GN�GN�GNkGN=s sn fs sn fN!,IShv�N (df  g=f7f)z-Get formatted forecast information for a cityNr-zError getting forecast: r7zForecast service error: zWeather forecast for r?rarbz: �forecast�datetimerrZr[r])�keyu📅 z: z Temperature: z.1fu °C (min: u °C, max: u°C) z Conditions: z )r r rHr<�split�append�list�items�sum�len�min�max�set�countrc)rr?r@rFrdr7� forecast_data� forecast_info�daily_forecasts�item�date� forecasts�f�temps�avg_temp�min_temp�max_temp� descriptions�most_common_descs r�get_forecast_info� WeatherService.get_forecast_infovs����T�_�_�-�-��!�6�6�t�4�P�P�F��&� �1�&��/�1B�C� .�-�-� #�J�J�x��4�M��-�'�1�-��2H�1I�J�.�-�-�4�M�&�4I�3J�"�]�[d�Me�Lf�fk�l�M�!�O�%�j�1���J�'�-�-�/��2���.�,.�O�)��%�,�,�T�2� 2� $(��(=�(=�(?�#@��$�#G���>G�H�i��=�)�)�4�i��H��u�:��E� �2���u�:���u�:��;D�D�)�Q�-� 0�)� �D�#&�s�<�'8�l�>P�>P�#Q� ��5���c�!2�2� ��#3�H�S�>��H�UX�>�Yc�dl�mp�cq�qw�!x�x� ��?�3C�2D�D�!I�I� �$H�!�&�&�(�I.�-�-�P�.��.I�� E�;.�-�-�-�s��G+�F9�G+�G�F<�G� G+�F?�G+�G�9 G+�G�G+� A4G�?G�3G�G �AG�' G+�3G�4G+�<G�?G+�G+� G�G+�G(�G � G(�$G+rWr)N�) rKrLrMrNrOr3rrrirPr�rQrRrrrTrTTs\��5�%�3�%�(�3�(�h�s�m�(�WZ�(�8&)�C�&)�x��}�&)�[^�&)�gj�&)�&)rrTc��# �[5n[S5 URSS5IShv�N n[S5 [U5 [S5 URSSS5IShv�N n[S5 [U5 gNWN7f) zTest the weather servicezTesting weather service...�London�GBNzCurrent Weather:z4 ================================================== r�z Forecast:)rT�printrir�)�weather_servicerhrzs r�test_weather_servicer��s|���$�&�O� � &�'�)�9�9�(�D�I�I�L� � �� �,�� � ��*�;�;�H�d�A�N�N�M� �+�� �-��J� O�s!�+B �B�;B �)B�*B �B �__main__)rO�asyncior,�loggingr�typingrrrr�dotenvr� basicConfig�INFO� getLoggerrKr2r rTr��runrRrr�<module>r�s����� �� �&�&��� � ����'�,�,�'� � � �8� $��=H�=H�~H)�H)�V�" �z�� �K�K�$�&�'�r

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/dbalencar/openai-chatbot-weather-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server