Skip to main content
Glama
server.cpython-313.pyc17 kB
� %��g�9� ��%SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKJrJ r J r J r SSK J r SSKJr SSKJr SSKJr SSKJrJr SSKJr SSKr\R6"\R8SS 9 \R:"S 5r0r\\ \4\!S '\""5r#\ \ \!S 'S r$Sr%/q&\RN"S5r(\((d\RSS5 Sr(\RUS\(35 \"S5r+\+RY5S\-\R\4Sj5r/\+Ra5S\S\ 4Sj5r1\+Re5S\-\Rf4Sj5r4\+Rk5S\ S\6\ \ 4S-S\Rn4Sj5r8\+Rs5S\-\Rt4Sj5r;S\<4Sjr=Sr>Sr?\+R�5S\ S\6S-S\-\R�\R�-\R�-4Sj5rDS rEg)!�N)�Dict�List�Optional�Set)� NmapProcess)� NmapParser)�InitializationOptions)�NotificationOptions�Server)�AnyUrlz4%(asctime)s - %(name)s - %(levelname)s - %(message)s)�level�formatznmap-mcp� scan_results� ongoing_scans�<��nmapz@Could not find nmap executable. Please ensure nmap is installed.z /usr/bin/nmapzUsing nmap executable at: �returnc��# �[R5VVs/sHaup[R"[ SU35SUR SS53SUR SS5SUR SS53SS 9PMc snn$s snnf7f) zw List available scan results as resources. Each scan result is exposed as a resource with a custom nmap:// URI scheme. z nmap://scan/zScan: �target�Unknownz Nmap scan of z - � timestampzapplication/json)�uri�name� description�mimeType)r�items�types�Resourcer �get)�scan_id� scan_datas �2/home/kali/mcp-servers/nmap/src/nmap_mcp/server.py�handle_list_resourcesr$,s����#/�"4�"4�"6� �#7� �G� �����g�Y�/�0��)�-�-��)�<�=�>�'� � � �h� �(J�'K�3�y�}�}�]h�js�Ot�Nu�v�'�  � #7� ��� �s�B �A(B� B rc��# �URS:wa[SUR35eURnUb7URS5nU[;a[ R "[USS9$[SU35e7f)z_ Read a specific scan result by its URI. The scan ID is extracted from the URI path component. rzUnsupported URI scheme: �/���indent�Scan result not found: )�scheme� ValueError�path�lstripr�json�dumps)rr!s r#�handle_read_resourcer1<sx���  �z�z�V���3�C�J�J�<�@�A�A��h�h�G����.�.��%�� �l� "��:�:�l�7�3�A�>� >� �.�w�i�8� 9�9�s�A=A?c ��# �[R"SS[R"SSSS9[R"SSS S9/S 9/$7f) z2 List available prompts related to nmap scanning. � analyze-scanzAnalyze an nmap scan resultr!zID of the scan to analyzeT)rr�required�focusz'Focus area (security/services/overview)F)rr� arguments)r�Prompt�PromptArgument��r#�handle_list_promptsr;LsX��� � � ��5��$�$�"� ;�!�� �$�$� � I�"�� � � ��s�AArr6c���# �US:wa[SU35eU(aSU;a [S5eUSnURSS5nU[;a[SU35e[UnSnUS :XaS nO US :XaS nOS n[R"SURSS53[R "S[R "SSUS[R"USS93S9S9/S9$7f)z4 Generate a prompt for analyzing nmap scan results. r3zUnknown prompt: r!zMissing scan_id argumentr5�overviewr*��securityz6Focus on security vulnerabilities and potential risks.�servicesz9Focus on identifying running services and their versions.z/Provide a general overview of the scan results.zAnalyze nmap scan results for rr�user�textz)Analyze the following nmap scan results. z r'r(��typerB)�role�content)r�messages) r,r rr�GetPromptResult� PromptMessage� TextContentr/r0)rr6r!r5r"� focus_prompts r#�handle_get_promptrLds ��� �~���+�D�6�2�3�3� � ��2��3�4�4�� �"�G� �M�M�'�:� .�E��l�"��2�7�)�<�=�=��W�%�I��L� � ��O� � �*� �R� �H� � � � �4�Y�]�]�8�Y�5W�4X�Y� � � ���)�)��D�\�N�RV�W[�Wa�Wa�bk�tu�Wv�Vw�x�� � � � �s�C#C%c ���# �[R"SSSSSS.SSS.S.S /S .S 9[R"S S SSSSS.0S/S .S 9[R"SSS0S.S 9/$7f)z) List available tools for nmap scanning. � run-nmap-scanz%Run an nmap scan on specified targets�object�stringz<Target host or network (e.g., 192.168.1.1 or 192.168.1.0/24))rDrz"Nmap options (e.g., -sV -p 1-1000))r�optionsr)rD� propertiesr4)rr� inputSchema�get-scan-detailsz.Get detailed information about a specific scanr!zID of the scan to retrieve�list-all-scanszList all available scan results)rDrR)r�Toolr9r:r#�handle_list_toolsrW�s���� � � � �?� �'/�@~��(0�Ae�f��&�J� � � � � �#�H� ���A]�^��'�K� � � � � �!�9� � �� �1 � �s�AAc��[R"5n[Vs/sHoU- [:dMUPM snq[[5[:$s snf)z(Check if we're exceeding the rate limit.)�time�last_scan_times�RATE_LIMIT_PERIOD�len�RATE_LIMIT_MAX_SCANS)� current_time�ts r#�check_rate_limitr`�sI���9�9�;�L�#2�Z�/�Q�A�5E�HY�5Y�q�/�Z�O� �� �"6� 6�6��[s �A�Ac�T�[R[R"55 g)z-Add current timestamp to track rate limiting.N)rZ�appendrYr9r:r#�add_scan_timestamprc�s�����4�9�9�;�'r:c ���[SS/nU(a!UR5nURU5 URU5 [R SSR U535 [R"USSSS9nURS4$![Ra6nSS URS URRS S S 934sSnA$SnAf[anS[U54sSnA$SnAff=f)zHRun nmap directly using subprocess instead of relying on python-libnmap.z-oX�-zExecuting nmap command: � TF)�capture_outputrB�checkNznmap failed with exit code z: �utf-8�replace��errors)� NMAP_PATH�split�extendrb�logger�info�join� subprocess�run�stdout�CalledProcessError� returncode�stderr�decode� Exception�str)rrQ�cmd� option_args�process�es r#�run_nmap_directlyr��s�����%��%�� �!�-�-�/�K� �J�J�{� #� � � �6��� � �.�s�x�x��}�o�>�?��.�.� ����  ���~�~�t�#�#�� � (� (�p��2�1�<�<�.��1�8�8�?�?�SZ�cl�?�Cm�Bn�o�o�o�� ���S��V�|����s0�B B�C9�$+C�C9� C9�" C4�.C9�4C9c�� # �U(dUS:wa [S5eUS:XGa�URS5nURSS5nU(d [S5eUSU3nU[;a[R"S S S 9/$[ 5(d%[R"S S [ S [S3S 9/$[RU5 [5 [RSUSU35 [X#5upVU(aF[RSU35 [R"S SU3S 9/[RU5 $URSSS9n[ R""U5n['[(R*"55n UUUR,UR.V V V s/sH�n U R0U R2U R4V s/sH+n [7U S5(a U R8O ['U 5PM- sn U R:V s/sH=n U R<U R>U R@U RBU RDS.PM? sn S.PM� sn n n S.[FU '[HRJRLRO5IShv�N [RSUR.5SU 35 [R"S SUR.5SU 3S 9/[RU5 $US:XGa URS5n U (d [S 5eU [F;a[R"S S!U S"3S 9/$[FU n[SS#URS$/555n[SS%URS$/555n[R"S S&URS5S'U S(URS5S)URS*5S+[QURS$/55S,US-US.U S/3S 9/$US:XGa[F(d[R"S S0S 9/$/n[FRU5H�up�[QURS$/55nURWS1U 35 URWS2URS535 URWS3URS535 URWS4U35 URWS55 M� [R"S S6S7RYU5-S 9/$[S8U35e![$abn [RS['U 535 [R"S S['U 53S 9/sSn A [RU5 $Sn A ff=fs sn fs sn fs sn n n fGN![$abn [RS['U 535 [R"S S['U 53S 9/sSn A [RU5 $Sn A ff=f![RU5 f=f7f)9z3 Handle tool execution requests for nmap scanning. rUzMissing argumentsrNrrQz-sVzMissing target�:rBz[A scan with the same target and options is already running. Please wait for it to complete.rCzGRate limit exceeded. Please wait before starting another scan. Maximum z scans per z seconds.zStarting nmap scan on z with options zNmap scan failed: rirjrkzError parsing nmap results: Nr)�port�protocol�state�service�banner)�address�status� hostnamesr@)rrQr�hostszScan completed. Found z hosts. Scan ID: zError during nmap scan: rTr!zMissing scan_idz Scan with ID z not foundc3�R# �UHoRS5S:XdMSv� M g7f)r��up�N)r ��.0�hosts r#� <genexpr>�#handle_call_tool.<locals>.<genexpr>os#���^�%?�T�8�8�H�CU�Y]�C]�q�q�%?�s�'� 'r�c3�X# �UH n[URS/55v� M" g7f)r@N)r\r r�s r#r�r�ps%���_�D^�D�#�d�h�h�z�2�6�7�7�D^�s�(*zScan of z (ID: z): - Options: z - Timestamp: rz - Hosts: z (z up) - Total ports/services: z Use the nmap://scan/z resource to access full resultsz!No scans have been performed yet.z - Scan ID: z Target: z Options: z Hosts: r>zAvailable scans: � zUnknown tool: )-r,r rrrJr`r]r[�addrcrprqr��error�discardryr�parse_fromstringrzr{�uuid�uuid4�startedr�r�r�r��hasattrrr@r�r�r�r�r�r�server�request_context�session�send_resource_list_changedr\�sumrrbrr)rr6rrQ�scan_keyrurx� xml_string�parsedrr!r��hostnamer�r"�hosts_up� total_ports� scan_list� hosts_counts r#�handle_call_toolr��sB��� ��!1�1��,�-�-� ������x�(���-�-� �5�1����-�.� .��X�Q�w�i�(�� �}� $��!�!��v��� � �!�!��!�!��b�cw�bx�yD�EV�DW�W`�a��� �T ,� � � �h� '� � � �K�K�0����w�i�P� Q�/�v�?�N�F��� � �1�&��:�;��%�%�#�1�&��:���P � !� !�(� +�A �#�]�]�7�9�]�E� �#�4�4�Z�@���$�*�*�,�'�G�!�"�#�^�^�(!'� � �'�&!-��#$(�<�<�"&�+�+�-1�N�N�&�,:��.5�X�v�-F�-F�H�M�M�C�PX�M�Y�,:�&�,0�=�=� %�,9�� )0� � �,3�,<�,<�)0���+2�?�?�*1�.�.� �,9� %��$!-�'� %�L�� !�8�(�(�0�0�K�K�M� M� M� �K�K�0��V�\�\�1B�0C�CT�U\�T]�^� _��!�!��1�#�f�l�l�2C�1D�DU�V]�U^�_��� � !� !�(� +� �#� #��-�-� �*����.�/� /� �,� &��!�!��(�� ��<��� �!��)� ��^�Y�]�]�7�B�%?�^�^���_�I�M�M�RY�[]�D^�_�_� � � � ��� � � �h� 7�8��w�i�H#�#,�=�=��#;�"<�=%�%.�]�]�;�%?�$@�A!�!$�Y�]�]�7�B�%?�!@� A��H�:�N0�0;�}�=,�,3�9�4T� V� �  � � �!� !��|��!�!��<��� �� �".�"4�"4�"6� �G��i�m�m�G�R�8�9�K� � � �{�7�)�4� 5� � � �z�)�-�-��*A�)B�C� D� � � �{�9�=�=��+C�*D�E� F� � � �y�� �6� 7� � � �R� � #7� � � ��+�d�i�i� �.B�B� � � ��>�$��0�1�1��o� �� � �;�C��F�8�D�E��%�%�#�;�C��F�8�D����v � !� !�(� +��{ ��*&�� %���0 N��� � �L�L�3�C��F�8�<� =��!�!��3�C��F�8�<��� � � !� !�(� +�� �� � !� !�(� +�s��B0Y�3A>V<�1Y�&T9�.;V<�)*V2�2V(�V2�AV-� V2�"9V<�V9�A V<�<H=Y�9 V%�AV �V%�V<� Y� V%�%V<�( V2�2V<�< X(�AX#�X(� X+� Y�#X(�(X+�+Y�Yc��# �[RS5 [RRR 5IShv�N up[R UU[SS[R[50S9S95IShv�N SSS5IShv�N gNYNN !,IShv�N (df  g=f7f)NzStarting nmap MCP serverrz0.1.0)�notification_options�experimental_capabilities)� server_name�server_version� capabilities) rprq�mcpr��stdio� stdio_serverrtr �get_capabilitiesr )� read_stream� write_streams r#�mainr��s���� �K�K�*�+��z�z���,�,�.�.�2M�;��j�j� � � !�"�&�#�4�4�)<�)>�.0�5�� �  � � �/�.�.� �/�.�.�.�s[�AB>�B�B>�AB$�B � B$� B>�B"�B>� B$�"B>�$B;�*B- �+ B;�7B>)F�asyncior/r�rY�logging�shutilrs�typingrrrr�libnmap.processr�libnmap.parserr�mcp.server.modelsr � mcp.typesr� mcp.serverr r �pydanticr �mcp.server.stdior�� basicConfig�INFO� getLoggerrprr{�__annotations__�setrr[r]rZ�whichrmr�rqr��list_resources�listrr$� read_resourcer1� list_promptsr7r;� get_prompt�dictrHrL� list_toolsrVrW�boolr`rcr�� call_toolrJ� ImageContent�EmbeddedResourcer�r�r9r:r#�<module>r�sL��� � � �� ��,�,�'�%�3��2������'�,�,�/e�f� � � �:� &��!#� �d�3��9�o�"��%� �s�3�x�������� �L�L�� � �� �L�L�S�T��I�� � �(�� � 4�5� �������� �T�%�.�.�%9� �� ����� :�F� :�s� :�� :������4�� � �#5����.����'� �'��s�C�x�.�4�/�'� ���'��'�R����$��e�j�j�!1�$��$�L 7�$� 7�(� �:����s2� �s2���+�s2� �%� � �e�0�0� 0�5�3I�3I� I�J�s2��s2�j 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/imjdl/nmap-mcpserver'

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