Skip to main content
Glama

MCP Unified Server

by getfounded
sdk.cpython-312.pyc17.5 kB
� Q8�h�0����dZddlZddlZddlZddlZddlmZmZmZm Z m Z m Z ddl m Z ddlZddlmZddlmZGd�d�ZGd �d �ZGd �d �ZGd �d�ZGd�d�Zgd�Zy)z= MCP Tool Kit SDK - Easy integration for Python applications �N)�Dict�Any�Optional�List�Callable�Union)�ThreadPoolExecutor)�wraps)� MCPClientc �B�eZdZdZd dededeedeefd�Z d�Z d �Z y) � ToolResultz<Wrapper for tool execution results with status and metadata.N�success�data�error�metadatac�D�||_||_||_|xsi|_y�N�rrrr)�selfrrrrs �4C:\Users\JackThompson\python\mcp-tool-kit\app\sdk.py�__init__zToolResult.__init__s"���� ��� ��� � ��B�� �c��d|j�dt|jt�r|jdd�d�S|j�d�S)NzToolResult(success=z, data=�dz...))r� isinstancer�str�rs r�__repr__zToolResult.__repr__sM��$�T�\�\�N�'�Z�X\�Xa�Xa�cf�Mg�$�)�)�D�S�/�9w�w{�|�|�mq�mv�mv�9w�w{�|�|rc�`�|j|j|j|jd�S)Nrrrs r�to_dictzToolResult.to_dicts(���|�|��I�I��Z�Z�� � �  � r)NN) �__name__� __module__� __qualname__�__doc__�boolrrrrrrr �rrr r s<��F�'��'�C�'��� �'�X`�ae�Xf�'� }� rr c �r�eZdZdZ d&dededededef d�Zd�Zd �Z d �Z d �Z d ed e ee fdefd�Zd ed e ee fdefd�Zdee ee fdeefd�Zdee ee fdeefd�Zdee ee ffd�Zd edee ee ffd�Zdefd�Zdedefd�Zdefd�Zdeddfd�Zd'dedd fd!�Zd(d"�Zd#efd$�Zy%))� MCPToolKitSDKa. Enhanced SDK for integrating MCP tools into Python applications. Features: - Simplified tool discovery and usage - Async/sync support - Context managers for resource management - Batch operations - Event hooks and middleware - Built-in caching and retry logic � server_url� async_mode� retry_count�timeout� cache_ttlc���t|�|_||_||_||_||_i|_g|_i|_|r td��nd|_ tjd�|_ y)a] Initialize the SDK with enhanced configuration options. Args: server_url: MCP server URL async_mode: Enable async operations retry_count: Number of retries for failed operations timeout: Request timeout in seconds cache_ttl: Cache time-to-live in seconds � )� max_workersNr()r �clientr*r+r,r-�_cache� _middleware�_event_handlersr � _executor�logging� getLogger�logger)rr)r*r+r,r-s rrzMCPToolKitSDK.__init__1sg��$ � �+�� �$���&����� �"����� ����!���?I�+��;�t����'�'��8�� rc��|S)zContext manager entry.r&rs r� __enter__zMCPToolKitSDK.__enter__Os��� rc�V�|jr|jjd��yy)z"Context manager exit with cleanup.T��waitN�r5�shutdown�r�exc_type�exc_val�exc_tbs r�__exit__zMCPToolKitSDK.__exit__Ss#�� �>�>� �N�N� #� #�� #� .� rc��K�|S�w)zAsync context manager entry.r&rs r� __aenter__zMCPToolKitSDK.__aenter__Xs ����� �s�c��^K�|jr|jjd��yy�w)zAsync context manager exit.Tr<Nr>r@s r� __aexit__zMCPToolKitSDK.__aexit__\s'���� �>�>� �N�N� #� #�� #� .� �s�+-� tool_name�params�returnc �d�|�dtj|d����}|jdd�rT||jvrF|j|\}}||jzt j �kDrt d|ddi��S|jD] }|||�}� |jd|j�}d} t|�D]�} |jd ||�|jj||�} t| t�rtj| �n| } t| t �rd | vrt#| d ��|jdd�r#| t j �f|j|<|jd ||| �t d| �cS|jd ||| �t dd| ��S#tj$$r| } Y��wxYw#t"$rN} t| �} | |d z kr0|j&j)d | d z�d|�d|�d| ���Yd} ~ ��^Yd} ~ ��dd} ~ wwxYw)a? Call a tool with enhanced error handling and result wrapping. Args: tool_name: Name of the tool params: Tool parameters **kwargs: Additional options (cache, retry, etc.) Returns: ToolResult object with execution results �:T)� sort_keys�cache�cached)r�retryN� before_callr� after_call�zRetry �/z for z: F)r)�json�dumps�getr2r-�timer r3r+�range� _emit_eventr1� call_toolrr�loads�dict� Exception�JSONDecodeErrorr8�warning)rrIrJ�kwargs� cache_key� cached_data� timestamp� middlewarer+� last_error�attempt�resultr�es rr\zMCPToolKitSDK.call_toolbs��!�k��4�:�:�f��#E�"F�G� � �:�:�g�t� $��d�k�k�)A�%)�[�[��%;� "�K���4�>�>�)�D�I�I�K�7�!�$� �x��>N�O�O��*�*�J�� �6�2�F�+��j�j��$�*:�*:�;� �� ��[�)�G� �� � �� �6�B����.�.�y�&�A��"�1;�F�C�1H�4�:�:�f�-�f�D�!�$��-�'�T�/�'��W� �6�6� �:�:�g�t�,�.2�D�I�I�K�-@�D�K�K� �*�� � ��y�&�$�G�!�$��-�-�/*�@ ����)�V�Z�@��%��Z�8�8��)�+�+�"�!�D�"��� � ��V� ��[�1�_�,��K�K�'�'�&��1�� �Q�{�m�5�QZ�P[�[]�^_�]`�(a�b��-�� �s>� /G�:A F=�AG�=G�G�G�G� H/�!=H*�*H/c��K�tj�}|j|j|j||fi|���d{���S7��w)zAsync version of call_tool.N)�asyncio�get_event_loop�run_in_executorr5r\)rrIrJrb�loops r�call_tool_asynczMCPToolKitSDK.call_tool_async�sQ�����%�%�'��)�T�)�)� �N�N� �N�N� � �  � �  � � � �s�AA �A �A � operationsc ��g}|D]=}|j|d|dfi|jdi���}|j|��?|S)z� Execute multiple tool calls in batch. Args: operations: List of dicts with 'tool', 'params', and optional 'options' Returns: List of ToolResult objects �toolrJ�options)r\rX�append)rrq�results�opris r� batch_callzMCPToolKitSDK.batch_call�s[�����B�#�T�^�^��6� ��8� ���&�&��B�'��F� �N�N�6� "� ��rc ��K�|D�cgc],}|j|d|dfi|jdi�����.}}tj|��d{���Scc}w7� �w)z'Async batch execution with concurrency.rsrJrtN)rprXrl�gather)rrqrw�taskss r�batch_call_asynczMCPToolKitSDK.batch_call_async�sz����!�  � !�� !�D� � ��6� ��8� � ��&�&��B�'� � !� � ��^�^�U�+�+�+�� �,�s�A�1A�A�A� Ac�Z�|jdi�}|jr |jSgS)z*Get list of available tools with metadata.� list_tools)r\rr�rris rr~zMCPToolKitSDK.list_tools�s'����� �b�1��$�n�n�v�{�{�4�"�4rc�b�|j�}|D]}|jd�|k(s�|cSy)z/Get detailed information about a specific tool.�nameN)r~rX)rrI�toolsrss r� get_tool_infozMCPToolKitSDK.get_tool_info�s3�����!���D��x�x���9�,�� ��rrfc�:�|jj|�y)z7Add middleware to process parameters before tool calls.N)r3ru)rrfs r�add_middlewarezMCPToolKitSDK.add_middleware�s�� ����� �+r�event�handlerc�z�||jvrg|j|<|j|j|�y)zRegister event handler.N)r4ru)rr�r�s r�onzMCPToolKitSDK.on�s9�� ��,�,� ,�*,�D� � �� '� ���U�#�*�*�7�3rc���||jvr|j|D] } ||i|��� yy#t$r(}|jjd|���Yd}~�<d}~wwxYw)z"Emit event to registered handlers.zEvent handler error: N)r4r_r8r)rr��argsrbr�rjs rr[zMCPToolKitSDK._emit_event�sn�� �D�(�(� (��/�/��6��C��T�,�V�,�7� )��!�C��K�K�%�%�(=�a�S�&A�B�B��C�s�.� A�A�A�path�FileOperationsc��t||�S)z/Get file operations helper for a specific path.)r�)rr�s r�filezMCPToolKitSDK.file�s���d�D�)�)r� repo_path� GitOperationsc��t||�S)zGet git operations helper.)r�)rr�s r�gitzMCPToolKitSDK.git�s���T�9�-�-rc��t|�S)zGet web operations helper.)� WebOperationsrs r�webzMCPToolKitSDK.web�s ���T�"�"rr�c������fd�}|S)z"Dynamically generate tool methods.c�(���j�|�Sr)r\)rJr�rs ��r� tool_methodz.MCPToolKitSDK.__getattr__.<locals>.tool_method�s����>�>�$��/� /rr&)rr�r�s`` r� __getattr__zMCPToolKitSDK.__getattr__�s��� 0��rN)zhttp://localhost:8000F��i,��.)rKr�)r!r"r#r$rr%�intrr:rDrFrHrrr r\rprrxr|r~rr�rr�r�r[r�r�r�r�r&rrr(r($s��� �2� ���� 9��9��9�� 9� � 9� � 9�<�/� �/� <9�3�<9��S�#�X��<9�Z�<9�|  �s�  �D��c��N�  �Yc�  ��T�$�s�C�x�.�%9��d�:�>N��( ,��d�3��8�n�1E� ,�$�z�JZ� ,�5�D��c�3�h��0�5� �s��x��S�#�X��/G��,��,�4��4�h�4� C��C�*��*�!1�*�.�S�.�?�.�#� ��rr(c��eZdZdZd dedefd�Zddeedefd�Zdd edeede fd �Z dd edeede fd �Z ddeede fd �Z y)r�z+Simplified file operations using MCP tools.�sdk� base_pathc� �||_||_yr)r�r�)rr�r�s rrzFileOperations.__init__������"��rNr�rKc��|xs |j}|jjdd|i�}|jr |jSdS)zRead file contents.� read_filer��)r�r�r\rr)rr�� full_pathris r�readzFileOperations.read s@���*�D�N�N� ����#�#�K�&�)�1D�E��$�n�n�v�{�{�4�"�4r�contentc�x�|xs |j}|jjd||d��}|jS)zWrite content to file.� write_file)r�r�)r�r�r\r)rr�r�r�ris r�writezFileOperations.writes6���*�D�N�N� ����#�#�L�9�QX�2Y�Z���~�~�rc�n�|xs |j}|j|�}|j||z|�S)zAppend content to file.)r�r�r�)rr�r�r��currents rruzFileOperations.appends4���*�D�N�N� ��)�)�I�&���z�z�'�G�+�Y�7�7rc� �|xs |j}|jjddtjj |�i�}|j r-tjj|�}||jvSy)zCheck if file exists.�list_directoryr�F) r�r�r\�osr��dirnamer�basenamer)rr�r�ri�filenames r�existszFileOperations.existssf���*�D�N�N� ����#�#�$4�v�r�w�w���y�?Y�6Z�[�� �>�>��w�w�'�'� �2�H��v�{�{�*� *�rr�r) r!r"r#r$r(rrrr�r%r�rur�r&rrr�r�s���5�#�M�#�c�#�5��#��5�#�5� �S���� ���� 8�c�8��#��8�$�8� �8�C�=��D�rr�c�h�eZdZdZd dedefd�Zdefd�Zdded ee ede fd �Z dd eede fd �Z y)r�zSimplified git operations.r�r�c� �||_||_yr)r�r�)rr�r�s rrzGitOperations.__init__)r�rrKc��|jjdd|ji�}|jr |jSdS)zGet git status.� git_statusr�r�)r�r\r�rrrs r�statuszGitOperations.status-s6�����#�#�L�;����2O�P��$�n�n�v�{�{�4�"�4rN�message�filesc�~�|j|d�}|r||d<|jjd|�}|jS)zCreate a git commit.)r�r�r�� git_commit�r�r�r\r)rr�r�rJris r�commitzGitOperations.commit2s<��#�~�~�'�B�� �#�F�7�O����#�#�L�&�9���~�~�r�branchc�|�d|ji}|r||d<|jjd|�}|jS)zPush to remote.r�r��git_pushr�)rr�rJris r�pushzGitOperations.push:s=���t�~�~�.�� �%�F�8� ����#�#�J��7���~�~�rr�r) r!r"r#r$r(rrr�rrr%r�r�r&rrr�r�&s^��$�#�M�#�c�#�5��5� �c��(�4��9�*=�����8�C�=��D�rr�c �l�eZdZdZdefd�Zd dedeede eeffd�Z d ded e deede eeffd �Z y) r�zSimplified web operations.r�c��||_yr)r�)rr�s rrzWebOperations.__init__Fs ����rN�url�headersrKc��|dd�}|r||d<|jjd|�}|jr |jSdS)zMake GET request.�GET)r��methodr��fetchN�r�r\rr)rr�r�rJris rrXzWebOperations.getIsD����.�� � '�F�9� ����#�#�G�V�4��$�n�n�v�{�{�6�$�6rrc��|d|d�}|r||d<|jjd|�}|jr |jSdS)zMake POST request.�POST)r�r��bodyr�r�Nr�)rr�rr�rJris r�postzWebOperations.postQsF�����=�� � '�F�9� ����#�#�G�V�4��$�n�n�v�{�{�6�$�6rr) r!r"r#r$r(rrrrrrXrr�r&rrr�r�Csi��$��M��7�s�7�X�d�^�7�u�S�$�Y�?O�7�7��7�3�7��$��7�5�QT�VZ�QZ�K[�7rr�)r(r r�r�r�)r$rlr6r�rY�typingrrrrrr�concurrent.futuresr rV� functoolsr �app.toolkit_clientr r r(r�r�r��__all__r&rr�<module>r�sh����� � �=�=�1� ��(� � �*\�\�~ � �F��:7�7�0 ^�r

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/getfounded/mcp-tool-kit'

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