Skip to main content
Glama

Notion MCP Server

by ccabanillas
server.cpython-311.pyc11.7 kB
� �Jg�#� �6�ddlmZddlmZmZmZmZddlmZddl Z ddl Z ddl m Z ddl Z ddl mZmZddlmZddlmZddlZejej� ��ejd ��Zee��jjjZed z Ze���sed e�����ee��ed ��Ze j d��Z!e j d��Z"e!s e#d���e"s e#d���dZ$dZ%de!��de$d�Z&de'fd�Z(de)de)de'fd�Z*de)de'fd�Z+e�,��de-efd���Z,e�.��de)d edeeezfd!���Z.d"�Z/e0d#krddl1Z1e1j2e/����dSdS)$�)�Server)�Resource�Tool� TextContent�EmbeddedResource)�AnyUrlN)�datetime)�Any�Sequence)� load_dotenv)�Path)�level� notion_mcpz.envzNo .env file found at z notion-todo�NOTION_API_KEY�NOTION_DATABASE_IDz%NOTION_API_KEY not found in .env filez)NOTION_DATABASE_ID not found in .env filez 2022-06-28zhttps://api.notion.com/v1zBearer zapplication/json)� Authorizationz Content-TypezNotion-Version�returnc ��FK�tj��4�d{V��}|�t�dt�d�t dddd�gi����d{V��}|���|���cddd���d{V��S#1�d{V��swxYwYdS) z Fetch todos from Notion databaseNz /databases/z/query�sorts� created_time� descending)� timestamp� direction��headers�json)�httpx� AsyncClient�post�NOTION_BASE_URL� DATABASE_IDr�raise_for_statusr)�client�responses �H/Users/danhilse/Documents/dev/python/notion_mcp/src/notion_mcp/server.py� fetch_todosr&3sY����� �"�"��������f����� >� >�;� >� >� >���%3�%1�����%�  �  �  �  �  �  �  �  �� �!�!�#�#�#��}�}�����������������������������������s�A!B� B�B�task�whenc ��hK�tj��4�d{V��}|�t�d�tdt iddd|id�gd�d d |id �d d d�d�d�����d{V��}|���|���cddd���d{V��S#1�d{V��swxYwYdS)zCreate a new todo in NotionNz/pages� database_id�title�text�content��typer,)r/r+�select�name)r/r0�checkboxF�r/r2)�Task�When�Checkbox)�parent� propertiesr)rrrr rr!r"r)r'r(r#r$s r%� create_todor9Es������ �"�"��������f����� &� &� &��(�+�6�!(�+1�I�t�;L�"M�"M�!N��� !)�#)�4�.��� !+�$)�!�!� � ���%� � � � � � � � ��* �!�!�#�#�#��}�}���/��������������������������������s�A2B!�! B+�.B+�page_idc ��<K�tj��4�d{V��}|�t�d|��tddddd�ii����d{V��}|���|���cddd���d{V��S#1�d{V��swxYwYdS) z!Mark a todo as complete in NotionNz/pages/r8r6r2Tr3r)rr�patchr rr"r)r:r#r$s r%� complete_todor=`sY����� �"�"��������f����� 0� 0�w� 0� 0���� *�$(�!�!���&�  �  �  �  �  �  �  �  �� �!�!�#�#�#��}�}�����������������������������������s�AB � B�Bc ���K�tdddddd�dddd gd �d �d d gd����tdddigd����tdddigd����tddddddd�idgd����gS)zList available todo tools�add_todozAdd a new todo item�object�stringzThe todo task description)r/� descriptionz-When the task should be done (today or later)�today�later)r/rB�enum)r'r(r'r()r/r8�required)r1rB� inputSchema�show_all_todoszShow all todo items from Notion�show_today_todosz#Show today's todo items from Notionr=zMark a todo item as complete�task_idz+The ID of the todo task to mark as complete)r��r%� list_toolsrMrs���� ��-� �!)�'B��� !)�'V�!(�'� 2��� � �$�V�,��� � � �& �!�9� � ���� � � � �#�=� � ���� � � � � �6� �� (�'T� � �� '�K� � � � � �M4 �4rLr1� argumentsc��K�|dk�r t|t��std���|�d��}|�dd��}|std���|dvrtd��� t ||���d {V��}t d d |�d |�d ����gS#t j$rW}t� dt|������t d dt|���d����gcYd }~Sd }~wwxYw|dv�rq t���d {V��}g}|�dg��D]�}|d} |d| ddr | dddd dnd| dd| ddr| dddnd |d!d"�} |d#kr| d� ��d$kr��|� | ����t d tj|d%�&�����gS#t j$rW}t� dt|������t d d't|���d����gcYd }~Sd }~wwxYw|d(kr�t|t��std���|�d)��} | std*��� t!| ���d {V��}t d d+| �d ����gS#t j$rW}t� dt|������t d d,t|���d����gcYd }~Sd }~wwxYwtd-|�����).z%Handle tool calls for todo managementr?zInvalid argumentsr'r(rDzTask is required)rCrDzWhen must be 'today' or 'later'Nr,z Added todo: z (scheduled for �)r.zNotion API error: zError adding todo: z\ Please make sure your Notion integration is properly set up and has access to the database.)rHrI�resultsr8�idr4r+rr-�r6r2r5r0r1�unknownr)rRr'� completedr(�createdrIrC�)�indentzError fetching todos: r=rJzTask ID is requiredzMarked todo as complete (ID: zError completing todo: zUnknown tool: )� isinstance�dict� ValueError�getr9rr� HTTPError�logger�error�strr&�lower�appendr�dumpsr=) r1rNr'r(�result�e�todos�formatted_todos�todo�props�formatted_todorJs r%� call_toolrk�s����� �z����)�T�*�*� 2��0�1�1� 1��}�}�V�$�$���}�}�V�W�-�-��� 1��/�0�0� 0� �)� )� )��>�?�?� ?� �&�t�T�2�2�2�2�2�2�2�2�F���E��E�E�d�E�E�E����� �� �� � � � �L�L�6�c�!�f�f�6�6� 7� 7� 7���U�s�1�v�v�U�U�U����� � � � � � ����� ���� �7� 7� 7� �%�-�-�'�'�'�'�'�'�E� �O�� � �)�R�0�0� 7� 7���\�*���t�*�LQ�RX�M�Za�Lb�j�E�&�M�'�2�1�5�f�=�i�H�H�hj�!&�z�!2�:�!>�?D�V�}�X�?V�e�E�&�M�(�3�F�;�;�\e�#�N�3� "�"���-�-�-�.��2H�2N�2N�2P�2P�T[�2[�2[���&�&�~�6�6�6�6�����O�A�>�>�>����� �� �� � � � �L�L�6�c�!�f�f�6�6� 7� 7� 7���X�#�a�&�&�X�X�X����� � � � � � ����� ���� �� � ��)�T�*�*� 2��0�1�1� 1��-�-� �*�*��� 4��2�3�3� 3� �(��1�1�1�1�1�1�1�1�F���C��C�C�C����� �� �� � � � �L�L�6�c�!�f�f�6�6� 7� 7� 7���Y�3�q�6�6�Y�Y�Y����� � � � � � ����� ���� �,�d�,�,� -� -�-s^�>.B-�-D�<A D�D�D�DH#�#J �2A J�>J �J �*L�M.�A M)�#M.�)M.c��2K�ddlm}trtst d���|��4�d{V��\}}t �||t ������d{V��ddd���d{V��dS#1�d{V��swxYwYdS)zMain entry point for the serverr)� stdio_serverzHNOTION_API_KEY and NOTION_DATABASE_ID environment variables are requiredN)�mcp.server.stdiormrr!r[�server�run�create_initialization_options)rm� read_stream� write_streams r%�mainrtsM����-�-�-�-�-�-� �e��e��c�d�d�d��|�~�~� � � � � � � �!<�+�|��j�j� � � � 0� 0� 2� 2� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � ���� � � � � � s�=B� B�B�__main__)3� mcp.serverr� mcp.typesrrrr�pydanticr�osrr r�typingr r �dotenvr �pathlibr �logging� basicConfig�DEBUG� getLoggerr^�__file__r7� project_root�env_path�exists�FileNotFoundErrorro�getenvrr!r[�NOTION_VERSIONr rrZr&r`r9r=rM�listrkrt�__name__�asynciorprKrLr%�<module>r�s6�������������������� ������ � � � � � � � ������� � � � � � � � � � � � ��������������������'�-�(�(�(�(� �� �<� (� (���t�H�~�~�$�+�2� � �&� �������A� � �?�X�?�?� @� @�@� � �H���� �� � � �����+�,�,���b�i�,�-�-� ��>� �*�<� =� =�=��B� �*�@� A� A�A���-��0�~�/�/�&�$� � �� �4�����$�C��s��t�����6��������$�����6�$�t�*�6�6�6���6�p�����Z.�#�Z.�#�Z.�(�;�IY�;Y�2Z�Z.�Z.�Z.���Z.�x  �  �  � �z����N�N�N��G�K�����������rL

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/ccabanillas/notion-mcp'

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