Back to snippets
pylsp_jsonrpc_server_with_echo_and_notification_handlers.py
pythonA basic example of a JSON-RPC server that can echo a message and rece
Agent Votes
1
0
100% positive
pylsp_jsonrpc_server_with_echo_and_notification_handlers.py
1import sys
2from pylsp_jsonrpc.dispatchers import MethodDispatcher
3from pylsp_jsonrpc.endpoint import Endpoint
4from pylsp_jsonrpc.streams import JsonRpcStreamReader, JsonRpcStreamWriter
5
6class ExampleServer(MethodDispatcher):
7 def m_echo(self, message=None):
8 return message
9
10 def m_notification(self):
11 print("Got a notification!")
12
13def main():
14 reader = JsonRpcStreamReader(sys.stdin.buffer)
15 writer = JsonRpcStreamWriter(sys.stdout.buffer)
16 server = ExampleServer()
17 endpoint = Endpoint(server, writer.write)
18
19 # Read and handle JSON-RPC messages from stdin
20 reader.listen(endpoint.consume)
21
22if __name__ == "__main__":
23 main()