Skip to content

Add dynamic operation registration and dispatch - #70

Merged
long-nt-tran merged 3 commits into
nexus-rpc:mainfrom
long-nt-tran:dynamic-nexus-operations
Nov 23, 2026
Merged

long-nt-tran merged 3 commits into
nexus-rpc:mainfrom
long-nt-tran:dynamic-nexus-operations

Conversation

@long-nt-tran

@long-nt-tran long-nt-tran commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

What changed

Same as nexus-rpc/sdk-go#74, for Python.

I have it so that the dynamic handler is excluded when listing named operations because it doesn't have a name. Let me know if this is the correct approach.

Why

This will be useful in the future (namely MCP outbound to start).

Testing

Added new unit test suite, all tests pass.

longtran@Longs-MacBook-Pro:~/work/forks/nexus-rpc-sdk-python (branch: dynamic-nexus-operations)
$ uv run pytest
=========================================== test session starts ===========================================
platform darwin -- Python 3.13.13, pytest-9.0.3, pluggy-1.5.0
rootdir: /Users/longtran/work/forks/nexus-rpc-sdk-python
configfile: pyproject.toml
plugins: pretty-1.3.0, asyncio-1.3.0, cov-6.1.1
asyncio: mode=Mode.STRICT, debug=False, asyncio_default_fixture_loop_scope=None, asyncio_default_test_loop_scope=function
collected 133 items

tests/handler/test_async_operation.py .                                                             [  0%]
tests/handler/test_cancellation.py ..                                                               [  2%]
tests/handler/test_dynamic_decorator.py .......................                                     [ 19%]
tests/handler/test_handler_validates_service_handler_collection.py ....                             [ 22%]
tests/handler/test_invalid_usage.py .......                                                         [ 27%]
tests/handler/test_middleware.py ..                                                                 [ 29%]
tests/handler/test_operation_handler_runtime_behavior.py .                                          [ 30%]
tests/handler/test_request_routing.py ........                                                      [ 36%]
tests/handler/test_service_handler_decorator_collects_expected_operation_definitions.py ......      [ 40%]
tests/handler/test_service_handler_decorator_requirements.py .....                                  [ 44%]
tests/handler/test_service_handler_decorator_results_in_correctly_functioning_operation_factories.py . [ 45%]
.                                                                                                   [ 45%]
tests/handler/test_service_handler_decorator_selects_correct_service_name.py .......                [ 51%]
tests/handler/test_service_handler_decorator_validates_against_service_contract.py ................ [ 63%]
....                                                                                                [ 66%]
tests/handler/test_service_handler_decorator_validates_duplicate_operation_names.py .               [ 66%]
tests/handler/test_sync_operation_handler_decorator_creates_valid_operation_handler.py ..           [ 68%]
tests/service_definition/test_service_decorator_creates_expected_operation_declaration.py .         [ 69%]
tests/service_definition/test_service_decorator_selects_correct_service_name.py ....                [ 72%]
tests/service_definition/test_service_decorator_validation.py .                                     [ 72%]
tests/service_definition/test_service_definition_inheritance.py ......                              [ 77%]
tests/test_common.py ..                                                                             [ 78%]
tests/test_get_input_and_output_types.py ............                                               [ 87%]
tests/test_operation_service.py ..........                                                          [ 95%]
tests/test_type_errors.py .                                                                         [ 96%]
tests/test_util.py .....                                                                            [100%]
Results (0.93s):
       133 passed

Comment on lines +213 to +221
named_factories = {}
for name, handler_factory in operation_handler_factories_by_method_name.items():
# The service contract lists only named operations. Exclude the dynamic fallback
# so validation does not reject it as an extra operation.
operation = get_operation(handler_factory)
if operation is not None and operation.dynamic:
continue
named_factories[name] = handler_factory
operation_handler_factories_by_method_name = named_factories

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like dynamic operation handler factories are being added to operation_handler_factories_by_method_name, which forces us to exclude them in multiple places. Maybe instead, the operation_handler decorator can mark the dynamic factory with a private attribute and not attach an Operation, so it never enters that dict. A separate function could then find it. This would also let us drop the dynamic field from the public Operation dataclass.

@long-nt-tran
long-nt-tran force-pushed the dynamic-nexus-operations branch from 60763a3 to 50d1fb6 Compare November 20, 2026 19:35
@long-nt-tran
long-nt-tran requested a review from atol November 20, 2026 19:36
Comment thread src/nexusrpc/handler/_decorators.py Outdated
The handler must accept raw nexusrpc.LazyValue input.

.. warning::
Dynamic operation handler (setting dynamic=True) are experimental and may change in

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: handlers (plural).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call, since there's only supposed to be a single dynamic handler per service I changed the are to is instead

@long-nt-tran
long-nt-tran merged commit aafe25a into nexus-rpc:main Nov 23, 2026
6 checks passed
@long-nt-tran
long-nt-tran deleted the dynamic-nexus-operations branch November 23, 2026 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants