Skip to main content
Glama
mcp_server.cpython-310.pyc13.4 kB
o z�g]Q�@s�dZddlZddlZddlZddlmZmZmZmZm Z ddl m Z ej ej d�e�e�ZddlmZmZmZmZddlmZdd lmZGd d �d �ZdS) z� MCP Server Interface Implements the Model Context Protocol (MCP) server for the Persistent-Code tool. Exposes the knowledge graph functionality through well-defined tools. �N)�Dict�List�Optional�Any�Union)�FastMCP)�level�)�KnowledgeGraph� ComponentType�ComponentStatus�RelationshipType)� CodeAnalyzer)�ContextAssemblerc@s�eZdZdZdMdedefdd�Zdd �ZdNd efd d �Z  dOdedededededeee ff dd�Z   dPdede ede ede edeee ff dd�Z dQdededede eee fdeee ff dd �Z !dRd"ed#edeee ffd$d%�Z  &dSded'e eed(edeee ffd)d*�Z  +dTd,ed-e eed.edeee ffd/d0�Z dUded1ed2edeee ffd3d4�Z  dVd5e eee fd6e edeee ffd7d8�Z 9 :dWd;ed.edeee ffd<d=�Z  >dXd?ed@e eedAedeee ffdBdC�Z >dYdedAedeee ffdDdE�Z  &dSdFe eedGedeee ffdHdI�Z  dVdedJe ede edeee ffdKdL�ZdS)Z�PersistentCodeMCPz/MCP server for Persistent-Code knowledge graph.�defaultN� project_name� storage_dircCsX||_|p tj�t��d�|_td�|_t||�|_ t |j �|_ t |j �|_ |��dS)z�Initialize the MCP server. Args: project_name: Name of the project storage_dir: Directory to store persistent data �storagezpersistent-codeN)r�os�path�join�getcwdrr�mcpr �knowledge_graphr� code_analyzerr�context_assembler�_register_tools)�selfrr�r�U/Users/sparshdrolia/Sparsh personal/persistent-code-mcp/persistent_code/mcp_server.py�__init__s     zPersistentCodeMCP.__init__cCs�|j��|j�|j��|j�|j��|j�|j��|j�|j��|j�|j��|j�|j��|j�|j��|j �|j��|j �|j��|j �|j��|j �|j��|j �|j��|j�dS)zRegister all MCP tools.N)r�tool� add_component�update_component�add_relationship� get_component�find_related_components� search_code� update_status�get_project_status�find_next_tasks�prepare_context�continue_implementation�get_implementation_plan� analyze_code)rrrr r3sz!PersistentCodeMCP._register_tools�stdio� transportcCs|jj|d�dS)zqRun the MCP server. Args: transport: Transport protocol ('stdio' or 'http') )r1N)r�run)rr1rrr r2LszPersistentCodeMCP.run�planned�� code_text�component_type�name�status� description�returnc �s|�z#t|�}t|�}|jj|||||d�}d|d|�d|�d|��d�WSty=} z dt| �d�WYd } ~ Sd } ~ ww) a�Add a new code component to the graph. Args: code_text: The actual code component_type: Type of component (file, class, function, method, variable) name: Name of the component status: Implementation status (planned, partial, implemented) description: Semantic description of the component Returns: result: Result including component_id )r7r6r5r8r9T�Added z 'z ' with ID )�success� component_id�messageF�r<�errorN)r r rr#� Exception�str) rr5r6r7r8r9Z comp_type� comp_statusr=�errr r#Xs*�� ����zPersistentCodeMCP.add_componentr=c �s��z'd}|r t|�}|jj||||d�}|rdd|��d�WSdd|�d�d �WStyA}z dt|�d �WYd}~Sd}~ww) aYUpdate an existing component. Args: component_id: ID of the component to update code_text: New code text (if changed) status: New implementation status (if changed) description: New description (if changed) Returns: result: Success status N)r=r5r8r9TzUpdated component �r<r>F� Component � not foundr?)r rr$rArB)rr=r5r8r9rCr<rDrrr r$�s0��� ����z"PersistentCodeMCP.update_component� source_id� target_id�relationship_type�metadatac �s��z&t|�}|jj||||d�}|r"d|d|�d|�d|��d�WSddd �WSty@}z dt|�d �WYd }~Sd }~ww) a�Create a relationship between components. Args: source_id: ID of the source component target_id: ID of the target component relationship_type: Type of relationship (imports, calls, inherits, contains, depends_on) metadata: Additional metadata for the relationship Returns: result: Result including relationship_id )rHrIrJrKTr;z relationship from � to )r<�relationship_idr>Fz One or both components not foundr?N)r rr%rArB)rrHrIrJrKZrel_typerMrDrrr r%�s.�������z"PersistentCodeMCP.add_relationshipF� identifier�include_relatedc �s��z4|j�||�}|rd|d�WS|jj|dd�}|r,|j�|dd|�}d|d�WSdd|�d �d �WStyN}z dt|�d �WYd }~Sd }~ww) z�Retrieve a component by ID or name. Args: identifier: Component ID or name include_related: Whether to include related components Returns: result: Component details T)r<� componentr )�query�limitr�idFz Component 'z ' not foundr?N)rr&r(rArB)rrNrOrP�search_resultsrDrrr r&�s8� �� �� ����zPersistentCodeMCP.get_componentr �relationship_types�depthc ��p�zd}|r dd�|D�}|jj|||d�}d|t|�d�WSty7}z dt|�d�WYd}~Sd}~ww) aKFind components related to a given component. Args: component_id: ID of the component relationship_types: Types of relationships to consider depth: How many levels of relationships to traverse Returns: result: List of related components NcS�g|]}t|��qSr)r )�.0�rtrrr � <listcomp>(�z=PersistentCodeMCP.find_related_components.<locals>.<listcomp>)r=rUrVT)r<�related_components�countFr?)rr'�lenrArB)rr=rUrVZ rel_types�relatedrDrrr r'�(������z)PersistentCodeMCP.find_related_components� rQ�component_typesrRc �rW) aSearch the codebase semantically. Args: query: Search query component_types: Types of components to search limit: Maximum number of results Returns: result: Ranked list of matching components NcSrXr)r )rY�ctrrr r[Or\z1PersistentCodeMCP.search_code.<locals>.<listcomp>)rQrcrRT)r<�matchesr^Fr?)rr(r_rArB)rrQrcrRZ comp_typesrerDrrr r(=razPersistentCodeMCP.search_code� new_status�notesc �s��z%t|�}|jj|||d�}|rdd|�d|��d�WSdd|�d�d �WSty?}z dt|�d �WYd }~Sd }~ww) aLUpdate implementation status of a component. Args: component_id: ID of the component new_status: New implementation status (planned, partial, implemented) notes: Optional notes about the status change Returns: result: Update confirmation )r=rfrgTzUpdated status of rLrEFrFrGr?N)r rr)rArB)rr=rfrgr8r<rDrrr r)hs*��� ����zPersistentCodeMCP.update_status�filters�groupingc �sR�z|jj||d�}d|d�WSty(}z dt|�d�WYd}~Sd}~ww)aRetrieve implementation status across the project. Args: filters: Filters to apply to components grouping: How to group the results (e.g., by type) Returns: result: Status summary )rhriT)r<r8Fr?N)rr*rArB)rrhrir8rDrrr r*�s� �����z$PersistentCodeMCP.get_project_status� dependencies�� priority_typec �sX�z|jj||d�}d|t|�d�WSty+}z dt|�d�WYd}~Sd}~ww)aSuggest logical next components to implement. Args: priority_type: How to prioritize tasks (dependencies, complexity) limit: Maximum number of tasks to suggest Returns: result: List of suggested tasks )rlrRT)r<�tasksr^Fr?N)rr+r_rArB)rrlrRrmrDrrr r+�s � �����z!PersistentCodeMCP.find_next_tasks��task_description�relevant_components� max_tokensc �sr�z|jj|||d�}d|t|d�t|d�t|d�d�WSty8}z dt|�d�WYd }~Sd }~ww) aJAssemble minimal context for a specific task. Args: task_description: Description of the task relevant_components: List of component IDs known to be relevant max_tokens: Maximum tokens for the context Returns: result: Assembled context )rorprqT�primary_componentsr]� summaries)r<�contextZ primary_countZ related_countZ summary_countFr?N)rr,r_rArB)rrorprqrtrDrrr r,�s&��   ����z!PersistentCodeMCP.prepare_contextc ��j�z|jj||d�}d|vrd|dd�WSd|d�WSty4}z dt|�d�WYd}~Sd}~ww)aProvide context to continue implementing a component. Args: component_id: ID of the component to continue implementing max_tokens: Maximum tokens for the context Returns: result: Implementation context )r=rqr@Fr?T)r<rtN)rr-rArB)rr=rqrtrDrrr r-��&� ������z)PersistentCodeMCP.continue_implementation� component_ids�dependencies_depthc �ru)aGenerate a plan for implementing pending components. Args: component_ids: List of component IDs to include dependencies_depth: How deep to analyze dependencies Returns: result: Ordered implementation plan )rwrxr@Fr?T)r<�planN)rr.rArB)rrwrxryrDrrr r.rvz)PersistentCodeMCP.get_implementation_plan� file_pathc �sh�z|jj|||d�}|jj|dd�}d||dd�WSty3}z dt|�d�WYd}~Sd}~ww) a7Analyze code and update the knowledge graph. Args: code_text: The code to analyze file_path: Path to the file (optional) component_id: ID of an existing component to update (optional) Returns: result: Analysis result )r5rzr=T)rOz1Successfully analyzed and updated knowledge graph)r<r=rPr>Fr?N)rr/rr&rArB)rr5rzr=rPrDrrr r/Cs&������zPersistentCodeMCP.analyze_code)rN)r0)r3r4)NNN)N)F)Nr )Nrb)r4)NN)rjrk)Nrn)rn)�__name__� __module__� __qualname__�__doc__rBr!rr2rrr#rr$r%�boolr&r�intr'r(r)r*r+r,r-r.r/rrrr rs������  �.�����  �3�����  �4���  �3�� ��  �)�� ��  �.����  �+���  ����  �%�� ��  �&���  �%� ��  �*���� �r)r~r�json�logging�typingrrrrrZmcp.server.fastmcpr� basicConfig�INFO� getLoggerr{�loggerrr r r r rrrrrrrrr �<module>s    

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/sparshdrolia/Persistent-code-mcp'

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