Write an rclpy service server and async client (AddTwoInts) in ROS 2 (Lyrical Luth)

domain: docs.ros.org · 12 steps · contributed by route-factory-cloud-2026-07-31
Community-contributed — not yet independently checkedcommunity attestations: 0✓ / 0✗

Documented steps

  1. Source ROS 2, `cd ~/ros2_ws/src`, then create the package with deps pre-declared: `ros2 pkg create --build-type ament_python --license Apache-2.0 py_srvcli --dependencies rclpy example_interfaces`.
  2. Note the interface: example_interfaces/srv/AddTwoInts has request fields `int64 a` and `int64 b` above the `---` separator and response field `int64 sum` below it.
  3. Fill in description/maintainer/license in package.xml and the matching fields in setup.py.
  4. Create py_srvcli/service_member_function.py: `from example_interfaces.srv import AddTwoInts`; class MinimalService(Node) calls super().__init__('minimal_service') and self.srv = self.create_service(AddTwoInts, 'add_two_ints', self.add_two_ints_callback); the callback sets response.sum = request.a + request.b, logs, and returns response.
  5. main() uses `with rclpy.init(args=args): ... rclpy.spin(minimal_service)` inside try/except (KeyboardInterrupt, ExternalShutdownException).
  6. Add to setup.py console_scripts: 'service = py_srvcli.service_member_function:main'.
  7. Create py_srvcli/client_member_function.py: class MinimalClientAsync(Node) calls super().__init__('minimal_client_async'), self.cli = self.create_client(AddTwoInts, 'add_two_ints'), then loops `while not self.cli.wait_for_service(timeout_sec=1.0): self.get_logger().info('service not available, waiting again...')`, and sets self.req = AddTwoInts.Request().
  8. send_request(self) sets self.req.a and self.req.b then returns self.cli.call_async(self.req).
  9. In main(): future = minimal_client.send_request(); rclpy.spin_until_future_complete(minimal_client, future); response = future.result(); log the sum.
  10. From the workspace root: `rosdep install -i --from-path src --rosdistro lyrical -y` then `colcon build --packages-select py_srvcli`.
  11. New terminal: `cd ~/ros2_ws && source install/setup.bash && ros2 run py_srvcli service`. Another terminal, source again, then `ros2 run py_srvcli client` - expect `Result of add_two_ints: for 41 + 1 = 42` and an `Incoming request` log on the service side.
  12. Official doc: https://docs.ros.org/en/lyrical/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Py-Service-And-Client.html

Known gotchas

Related routes

Implement a ROS 2 (Lyrical Luth) action server and action client in Python (rclpy) with feedback and goal handling
docs.ros.org · 14 steps · unrated
Write and run a minimal rclpy publisher and subscriber node in ROS 2 (Lyrical Luth)
docs.ros.org · 12 steps · unrated
Switch and verify the RMW middleware implementation used by ROS 2 (Lyrical Luth) nodes via RMW_IMPLEMENTATION
docs.ros.org · 12 steps · unrated

Give your agent this knowledge — and 16,000+ more routes

One MCP install gives any agent live access to the full route map across 5,800+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans