{"id":17760857,"url":"https://github.com/microsoft/multilspy","last_synced_at":"2025-05-15T07:06:41.341Z","repository":{"id":252267898,"uuid":"839358595","full_name":"microsoft/multilspy","owner":"microsoft","description":"multilspy is a lsp client library in Python intended to be used to build applications around language servers.","archived":false,"fork":false,"pushed_at":"2025-04-20T04:51:41.000Z","size":271,"stargazers_count":327,"open_issues_count":48,"forks_count":58,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-05-15T00:08:27.056Z","etag":null,"topics":["ai","ai4code","artificial-intelligence","code-analysis","code-completion","code-generation","codegen","huggingface-transformers","language-server-client","language-server-protocol","large-language-model","large-language-models","llm","lsp","lsp-client","neurips","neurips-2023","program-synthesis","transformer"],"latest_commit_sha":null,"homepage":"https://www.microsoft.com/en-us/research/publication/guiding-language-models-of-code-with-global-context-using-monitors/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/microsoft.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":"CITATION.cff","codeowners":null,"security":"SECURITY.md","support":"SUPPORT.md","governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-08-07T12:58:24.000Z","updated_at":"2025-05-14T18:27:13.000Z","dependencies_parsed_at":"2024-08-08T18:50:14.512Z","dependency_job_id":"88624f38-2960-493f-8142-682a745a1842","html_url":"https://github.com/microsoft/multilspy","commit_stats":null,"previous_names":["microsoft/multilspy"],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microsoft%2Fmultilspy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microsoft%2Fmultilspy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microsoft%2Fmultilspy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/microsoft%2Fmultilspy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/microsoft","download_url":"https://codeload.github.com/microsoft/multilspy/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254254038,"owners_count":22039792,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","ai4code","artificial-intelligence","code-analysis","code-completion","code-generation","codegen","huggingface-transformers","language-server-client","language-server-protocol","large-language-model","large-language-models","llm","lsp","lsp-client","neurips","neurips-2023","program-synthesis","transformer"],"created_at":"2024-10-26T19:13:44.233Z","updated_at":"2025-05-15T07:06:36.330Z","avatar_url":"https://github.com/microsoft.png","language":"Python","readme":"[![PyPI - Version](https://img.shields.io/pypi/v/multilspy)](https://pypi.org/project/multilspy/)\n# Multilspy: LSP client library in Python to build applications around language servers\n\n## Introduction\nThis repository hosts `multilspy`, a library developed as part of research conducted for NeruIPS 2023 paper titled [\"Monitor-Guided Decoding of Code LMs with Static Analysis of Repository Context\"](https://neurips.cc/virtual/2023/poster/70362) ([\"Guiding Language Models of Code with Global Context using Monitors\"](https://arxiv.org/abs/2306.10763) on Arxiv). The paper introduces Monitor-Guided Decoding (MGD) for code generation using Language Models, where a monitor uses static analysis to guide the decoding, ensuring that the generated code follows various correctness properties, like absence of hallucinated symbol names, valid order of method calls, etc. For further details about Monitor-Guided Decoding, please refer to the paper and GitHub repository [microsoft/monitors4codegen](https://github.com/microsoft/monitors4codegen).\n\n`multilspy` is a cross-platform library designed to simplify the process of creating language server clients to query and obtain results of various static analyses from a wide variety of language servers that communicate over the [Language Server Protocol](https://microsoft.github.io/language-server-protocol/). It is easily extensible to support any [language that has a Language Server](https://microsoft.github.io/language-server-protocol/implementors/servers/) and we aim to continuously add support for more language servers and languages.\n\n[Language servers]((https://microsoft.github.io/language-server-protocol/overviews/lsp/overview/)) are tools that perform a variety of static analyses on code repositories and provide useful information such as type-directed code completion suggestions, symbol definition locations, symbol references, etc., over the [Language Server Protocol (LSP)](https://microsoft.github.io/language-server-protocol/overviews/lsp/overview/). Since LSP is language-agnostic, `multilspy` can provide the results for static analyses of code in different languages over a common interface.\n\n`multilspy` intends to ease the process of using language servers, by handling various steps in using a language server:\n* Automatically handling the download of platform-specific server binaries, and setup/teardown of language servers\n* Handling JSON-RPC based communication between the client and the server\n* Maintaining and passing hand-tuned server and language specific configuration parameters\n* Providing a simple API to the user, while executing all steps of server-specific protocol steps to execute the query/request.\n\nSome of the analysis results that `multilspy` can provide are:\n- Finding the definition of a function or a class ([textDocument/definition](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_definition))\n- Finding the callers of a function or the instantiations of a class ([textDocument/references](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_references))\n- Providing type-based dereference completions ([textDocument/completion](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_completion))\n- Getting information displayed when hovering over symbols, like method signature ([textDocument/hover](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_hover))\n- Getting list/tree of all symbols defined in a given file, along with symbol type like class, method, etc. ([textDocument/documentSymbol](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_documentSymbol))\n- Please create an issue/PR to add any other LSP request not listed above\n\n## Installation\nIt is ideal to create a new virtual environment with `python\u003e=3.10`. To create a virtual environment using conda and activate it:\n```\nconda create -n multilspy_env python=3.10\nconda activate multilspy_env\n```\nFurther details and instructions on creation of Python virtual environments can be found in the [official documentation](https://docs.python.org/3/library/venv.html). Further, we also refer users to [Miniconda](https://docs.conda.io/en/latest/miniconda.html), as an alternative to the above steps for creation of the virtual environment.\n\nTo install `multilspy` using pip, execute the following command:\n```\npip install multilspy\n```\n\n## Supported Languages\n`multilspy` currently supports the following languages:\n| Code Language | Language Server |\n| --- | --- | \n| java | Eclipse JDTLS |\n| python | jedi-language-server |\n| rust | Rust Analyzer |\n| csharp | OmniSharp / RazorSharp |\n| typescript | TypeScriptLanguageServer |\n| javascript | TypeScriptLanguageServer |\n| go | gopls |\n| dart | Dart |\n| ruby | Solargraph |\n| kotlin | KotlinLanguageServer |\n\n\n## Usage\nExample usage:\n```python\nfrom multilspy import SyncLanguageServer\nfrom multilspy.multilspy_config import MultilspyConfig\nfrom multilspy.multilspy_logger import MultilspyLogger\n...\nconfig = MultilspyConfig.from_dict({\"code_language\": \"java\"}) # Also supports \"python\", \"rust\", \"csharp\", \"typescript\", \"javascript\", \"go\", \"dart\", \"ruby\"\nlogger = MultilspyLogger()\nlsp = SyncLanguageServer.create(config, logger, \"/abs/path/to/project/root/\")\nwith lsp.start_server():\n    result = lsp.request_definition(\n        \"relative/path/to/code_file.java\", # Filename of location where request is being made\n        163, # line number of symbol for which request is being made\n        4 # column number of symbol for which request is being made\n    )\n    result2 = lsp.request_completions(\n        ...\n    )\n    result3 = lsp.request_references(\n        ...\n    )\n    result4 = lsp.request_document_symbols(\n        ...\n    )\n    result5 = lsp.request_hover(\n        ...\n    )\n    ...\n```\n\n`multilspy` also provides an asyncio based API which can be used in async contexts. Example usage (asyncio):\n```python\nfrom multilspy import LanguageServer\n...\nlsp = LanguageServer.create(...)\nasync with lsp.start_server():\n    result = await lsp.request_definition(\n        ...\n    )\n    ...\n```\n\nThe file [src/multilspy/language_server.py](src/multilspy/language_server.py) provides the `multilspy` API. Several tests for `multilspy` present under [tests/multilspy/](tests/multilspy/) provide detailed usage examples for `multilspy`. The tests can be executed by running:\n```bash\npytest tests/multilspy\n```\n\n## Use of `multilspy` in AI4Code Scenarios like Monitor-Guided Decoding\n`multilspy` provides all the features that language-server-protocol provides to IDEs like VSCode. It is useful to develop toolsets that can interface with AI systems like Large Language Models (LLM). \n### [Monitor-Guided Decoding](https://github.com/microsoft/monitors4codegen)\nOne such usecase is Monitor-Guided Decoding, where `multilspy` is used to find results of static analyses like type-directed completions, to guide the token-by-token generation of code using an LLM, ensuring that all generated identifier/method names are valid in the context of the repository, significantly boosting the compilability of generated code. MGD also demonstrates use of `multilspy` to create monitors that ensure all function calls in LLM generated code receive correct number of arguments, and that functions of an object are called in the right order following a protocol (like not calling \"read\" before \"open\" on a file object).\n\n### Multilspy in other usecases\n* [\"Fix the Tests: Augmenting LLMs to Repair Test Cases with Static Collector and Neural Reranker,\" in 2024 IEEE 35th International Symposium on Software Reliability Engineering (ISSRE)](https://github.com/SQUARE-RG/SynTeR)\n* [Tutorial on obtaining python completions with multilspy](https://medium.com/@techhara/python-obtain-completions-3db4d2479b82)\n* Gathering and utilizing repository-wide context for repository-level coding agents\n\n## Frequently Asked Questions (FAQ)\n### ```asyncio``` related Runtime error when executing the tests for MGD\nIf you get the following error:\n```\nRuntimeError: Task \u003cTask pending name='Task-2' coro=\u003c_AsyncGeneratorContextManager.__aenter__() running at\n    python3.8/contextlib.py:171\u003e cb=[_chain_future.\u003clocals\u003e._call_set_state() at\n    python3.8/asyncio/futures.py:367]\u003e got Future \u003cFuture pending\u003e attached to a different loop python3.8/asyncio/locks.py:309: RuntimeError\n```\n\nPlease ensure that you create a new environment with Python ```\u003e=3.10```. For further details, please have a look at the [StackOverflow Discussion](https://stackoverflow.com/questions/73599594/asyncio-works-in-python-3-10-but-not-in-python-3-8).\n\n## Citing Multilspy\nIf you're using Multilspy in your research or applications, please cite using this BibTeX:\n```\n@inproceedings{NEURIPS2023_662b1774,\n author = {Agrawal, Lakshya A and Kanade, Aditya and Goyal, Navin and Lahiri, Shuvendu and Rajamani, Sriram},\n booktitle = {Advances in Neural Information Processing Systems},\n editor = {A. Oh and T. Naumann and A. Globerson and K. Saenko and M. Hardt and S. Levine},\n pages = {32270--32298},\n publisher = {Curran Associates, Inc.},\n title = {Monitor-Guided Decoding of Code LMs with Static Analysis of Repository Context},\n url = {https://proceedings.neurips.cc/paper_files/paper/2023/file/662b1774ba8845fc1fa3d1fc0177ceeb-Paper-Conference.pdf},\n volume = {36},\n year = {2023}\n}\n```\n\n## Contributing\n\nThis project welcomes contributions and suggestions.  Most contributions require you to agree to a\nContributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us\nthe rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.\n\nWhen you submit a pull request, a CLA bot will automatically determine whether you need to provide\na CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions\nprovided by the bot. You will only need to do this once across all repos using our CLA.\n\nThis project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).\nFor more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or\ncontact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.\n\n## Trademarks\n\nThis project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft \ntrademarks or logos is subject to and must follow \n[Microsoft's Trademark \u0026 Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general).\nUse of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.\nAny use of third-party trademarks or logos are subject to those third-party's policies.\n","funding_links":[],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmicrosoft%2Fmultilspy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmicrosoft%2Fmultilspy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmicrosoft%2Fmultilspy/lists"}