To extend @ahmet’s reply, what the gateway does when loading a plugin bundle is to take the contents of its manifest file and inject it into the API definition. To override this behavior, you could simply do this manually: open your manifest file, locate the custom_middleware, copy it to the API definition.
My custom plugin is actually a Python plugin, so I’m not using RPCs. @matiasb I can do this, but how to make the gateway load the plugin in the first place so that the created object is available?