Skip to main content
Glama
server.cpython-312.pyc12.1 kB
� ���h����UdZddlZddlmZddlmZddlmZddlm Z ddl m Z m Z dd l mZmZmZmZmZdd lmZej*e�Zdaeeed <d efd �Zed�Zej9�d efd��Zej9�e dd��fded efd��Zej9�e dd��fded efd��Z ej9�e dd��e dddd��fdede!d efd��Z"ej9�e dd��fded efd ��Z#ej9�e dd!��e dd"��fded#ed efd$��Z$ej9�e dd%��fded efd&��Z%d'�Z&d(�Z'ed)k(re'�yy)*z-FastMCP server for background job management.�N)�Optional)�FastMCP)� ToolError)�Field�)�BackgroundJobConfig� load_config)� ExecuteOutput� KillOutput� ListOutput� ProcessOutput� StatusOutput)� JobManager� _job_manager�returnc�n�t�*t�}t|�atj d�tS)z.Get or create the global job manager instance.zInitialized JobManager)rr r�logger�info)�configs �V/Users/dylan/Workspace/mcp/servers/mcp-background-job/src/mcp_background_job/server.py�get_job_managerrs.�������!�&�)� �� � �,�-� ��zmcp-background-jobc���K� t�}|j��d{���}t|��S7�#t$r4}tj d|���t dt|�����d}~wwxYw�w)z�List all background jobs with their status. Returns a list of all background jobs, including their job ID, status, command, and start time. Jobs are sorted by start time (newest first). N)�jobszError listing jobs: zFailed to list jobs: )r� list_jobsr � Exceptionr�errorr�str)� job_managerr�es rrr"sn����:�%�'� � �*�*�,�,���t�$�$�-�� �:�� � �+�A�3�/�0��/��A��x�8�9�9��:�s0�A4�4�2�4�A4�4� A1�/A,�,A1�1A4.zJob ID to check)� description�job_idc��"K� t�}|j|��d{���}t|��S7�#t$rt d|�d���t $r7}t jd|�d|���t dt|�����d}~wwxYw�w)z�Get the current status of a background job. Args: job_id: The UUID of the job to check Returns: The current status of the job (running, completed, failed, or killed) N��status�Job � not foundzError getting job status for �: zFailed to get job status: ) r�get_job_statusr�KeyErrorrrrrr)r"r� job_statusr s rr)r)2s�����?�%�'� �&�5�5�f�=�=� ��:�.�.�>�� �3��$�v�h�j�1�2�2� �?�� � �4�V�H�B�q�c�B�C��4�S��V�H�=�>�>��?�s1�B�5�3�5�B�5� B �2B�B � BzJob ID to get output fromc��K� t�}|j|��d{���}|S7�#t$rtd|�d���t$r7}t j d|�d|���tdt|�����d}~wwxYw�w)z�Get the complete stdout and stderr output of a job. Args: job_id: The UUID of the job to get output from Returns: ProcessOutput containing the complete stdout and stderr content Nr&r'zError getting job output for r(zFailed to get job output: )r�get_job_outputr*rrrrr)r"r� job_outputr s rr-r-Is�����?�%�'� �&�5�5�f�=�=� ���>�� �3��$�v�h�j�1�2�2� �?�� � �4�V�H�B�q�c�B�C��4�S��V�H�=�>�>��?�s1�B�+�)�+�B�+� B� 2A=�=B�BzJob ID to tail�2zNumber of lines to returni�)r!�ge�le�linesc��VK� t�}|j||��d{���}|S7�#t$rtd|�d���t$r}tdt |�����d}~wt $r7}tjd|�d|���tdt |�����d}~wwxYw�w)aGet the last N lines of stdout and stderr from a job. Args: job_id: The UUID of the job to tail lines: Number of lines to return (1-1000, default 50) Returns: ProcessOutput containing the last N lines of stdout and stderr Nr&r'zInvalid parameter: zError tailing job output for r(zFailed to tail job output: ) r�tail_job_outputr*r� ValueErrorrrrr)r"r2rr.r s rr4r4`s����� @�%�'� �&�6�6�v�u�E�E� ���F�� �3��$�v�h�j�1�2�2� �8��-�c�!�f�X�6�7�7�� �@�� � �4�V�H�B�q�c�B�C��5�c�!�f�X�>�?�?��@�s=�B)�,�*�,�B)�,� B&� A#�# B&�/2B!�!B&�&B)zShell command to execute�commandc���K� t�}|j|��d{���}t|��S7�#t$r}t dt |�����d}~wt $r@}dt |�vrt dt |�����t dt |�����d}~wt$r7}tjd|�d|���t d t |�����d}~wwxYw�w) z�Execute a command as a background job and return job ID. Args: command: Shell command to execute in the background Returns: ExecuteOutput containing the job ID (UUID) of the started job N)r"zInvalid command: zMaximum concurrent jobs limitzJob limit reached: zFailed to start job: zError executing command 'z': zFailed to execute command: ) r�execute_commandr r5rr� RuntimeErrorrrr)r6rr"r s rr8r8{s����� @�%�'� �"�2�2�7�;�;���F�+�+�<�� �6��+�C��F�8�4�5�5�� �>� *�c�!�f� 4��1�#�a�&��:�;� ;��3�C��F�8�<�=� =�� �@�� � �0�� ��Q�C�@�A��5�c�!�f�X�>�?�?��@�sH�C"�5�3�5�C"�5� C�A� C�!;B� C�(2C�C�C"zJob ID to interact withz Input to send to the job's stdin�inputc��K� t�}|j||��d{���}|S7�#t$rtd|�d���t$r8}dt |�vrtd|�d���tdt |�����d}~wt $r7}tjd|�d|���tdt |�����d}~wwxYw�w) aSend input to a job's stdin and return any immediate output. Args: job_id: The UUID of the job to interact with input: Text to send to the job's stdin Returns: ProcessOutput containing any immediate stdout/stderr output after sending input Nr&r'z not runningz' is not running and cannot accept inputzFailed to interact with job: zError interacting with job r() r�interact_with_jobr*rr9rrrr)r"r:r�interaction_resultr s rr<r<�s����� B�%�'� �#.�#@�#@���#O�O��!�!�P�� �3��$�v�h�j�1�2�2� �F� �C��F� "��d�6�(�*Q�R�S� S��;�C��F�8�D�E� E�� �B�� � �2�6�(�"�Q�C�@�A��7��A��x�@�A�A��B�s=�C�,�*�,�C�,� C� 3A?�? C� 2B=�=C�CzJob ID to killc���K� t�}|j|��d{���}t|��S7�#t$r7}tj d|�d|���t dt|�����d}~wwxYw�w)z�Kill a running background job. Args: job_id: The UUID of the job to terminate Returns: KillOutput indicating the result of the kill operation Nr$zError killing job r(zFailed to kill job: )r�kill_jobr rrrrr)r"r� kill_resultr s rr?r?�sv����9�%�'� �'�0�0��8�8� ���-�-�9�� �9�� � �)�&���A�3�7�8��.�s�1�v�h�7�8�8��9�s0�A8�5�3�5�A8�5� A5�2A0�0A5�5A8c��K�trGtjd�tj��d{���tjd�yy7��w)z+Cleanup function called on server shutdown.zShutting down JobManager...NzJobManager shutdown complete)rrr�shutdown�rr�cleanup_on_shutdownrD�s?������ � �1�2��#�#�%�%�%�� � �2�3��%�s�3A�A�Ac�|����ddl�ddl}ddl�tjtj d�j ��tjd��fd����fd�}|j|j|�|j|j|� tj�y#t$r1tjd��jt��Yyt$rL}tj!d |����jt���j#d �Yd}~yd}~wwxYw) z$Main entry point for the MCP server.rNz4%(asctime)s - %(name)s - %(levelname)s - %(message)s)�level�format�streamz"Starting MCP Background Job Serverc��X�K�t��d{����jd�y7��w)Nr)rD�exit)�syss�r�shutdown_handlerzmain.<locals>.shutdown_handler�s#�����!�#�#�#� ���� � $�s �*�(�*c�d��tjd|�d���j���y)NzReceived signal z, shutting down...)rr� create_task)�signum�frame�asynciorLs ��r�signal_handlerzmain.<locals>.signal_handler�s,���� � �&�v�h�.@�A�B����,�.�/rz,Received KeyboardInterrupt, shutting down...zServer error: r)rQ�signalrK�logging� basicConfig�INFO�stderrrr�SIGINT�SIGTERM�mcp�run�KeyboardInterruptrDrrrJ)rSrRr rQrLrKs @@@r�mainr]�s������� ����l�l�E��z�z��  �K�K�4�5��0� �M�M�&�-�-��0� �M�M�&�.�.�.�1� � ��� �� �+�� � �B�C�� � �'�)�*� ��� � �~�a�S�)�*�� � �'�)�*� ���� � ���s�B.�.7D;�'D;�/AD6�6D;�__main__)(�__doc__rT�typingr�fastmcpr�fastmcp.exceptionsr�pydanticrrrr �modelsr r r r r�servicer� getLogger�__name__rr�__annotations__rrZ�toolrrr)r-�intr4r8r<r?rDr]rCrr�<module>rks��3����(��4�V�V�� �� � �8� $��&*� �h�z�"�)�����"�#������ :�� :� � :�������):�;�?� �?��?� �?�,������)D�E�?� �?��?� �?�,������)9�:��r�'B�q�T�R�@� �@� �@��@� �@�4������*D�E�@� �@��@� �@�6������)B�C��s�(J�K�B� �B� �B��B� �B�:������)9�:�9� �9��9� �9�(4�$�N �z���F�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/dylan-gluck/mcp-background-job'

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