Skip to main content

Expose Django REST Framework serializers and viewsets as OpenAI / MCP tools with automatic JSON Schema generation

Project description

drf-llm-gateway

PyPI version Python versions License: MIT

Turn your existing Django REST Framework viewsets into tools an LLM can call — OpenAI function-calling format, MCP (Model Context Protocol) tool format, or plain JSON Schema — generated automatically from the serializers you already have.

from rest_framework import viewsets
from drf_llm_gateway import expose_as_tool


@expose_as_tool(actions=["list", "retrieve", "create"])
class ArticleViewSet(viewsets.ModelViewSet):
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer
from drf_llm_gateway import default_registry
from drf_llm_gateway.openai_tools import to_openai_tools

tools = to_openai_tools(default_registry)
# [{"type": "function", "function": {"name": "article_list", "description": "...",
#   "parameters": {"type": "object", "properties": {...}}}}, ...]

Why

Wiring an LLM agent up to call your existing API usually means hand-writing and hand-maintaining a parallel set of tool/function schemas — which drift out of sync with your serializers the moment either side changes. This package derives the schema from the serializer itself at generation time, so there is nothing to keep in sync: change a field on the serializer, regenerate the tools, done.

Features

  • Automatic JSON Schema generation from any DRF serializer — every standard field type, nested serializers, list serializers, related fields, choice fields, and more.
  • OpenAI function-calling format and MCP tool format exporters.
  • Tool registry — a decorator (@expose_as_tool) that registers a viewset's actions as callable tools without any schema duplication.
  • Runtime executor — validates arguments against the real serializer and dispatches to the real viewset action, respecting DRF permission and authentication classes.
  • Auth/permission metadata attached to every tool definition, so a gateway can decide what an agent identity may call before it calls it.
  • Schema versioning — every generated tool carries a content hash that changes whenever its schema changes, so consumers can detect drift.
  • CLI (generate_llm_tools management command) to export the full tool set as JSON for any external agent framework.
  • Fully typed, PEP 561 compatible, mypy --strict clean.

Installation

pip install drf-llm-gateway

Requires Python 3.10+, Django 4.2+, and Django REST Framework 3.14+.

Quick Start

See docs/quickstart.md for a complete walkthrough, including calling a tool end-to-end from a validated argument dict.

Documentation

Full documentation: https://mahmoudgshake.github.io/MahmoudPackages/drf-llm-gateway

Contributing

Contributions are welcome — see CONTRIBUTING.md.

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf_llm_gateway-1.0.0.tar.gz (46.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

drf_llm_gateway-1.0.0-py3-none-any.whl (24.2 kB view details)

Uploaded Python 3

File details

Details for the file drf_llm_gateway-1.0.0.tar.gz.

File metadata

  • Download URL: drf_llm_gateway-1.0.0.tar.gz
  • Upload date:
  • Size: 46.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for drf_llm_gateway-1.0.0.tar.gz
Algorithm Hash digest
SHA256 90c135b1f964aeda5daa332d17922ac03f9a9aa4e822d267f7a36745934ad0c6
MD5 b66e0f1dc55ed5833166f20616fc44ab
BLAKE2b-256 119500427afa3eae977c25459d3afdcfa5c8c1dca6f3da227ab3f8dc770d6b7f

See more details on using hashes here.

File details

Details for the file drf_llm_gateway-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for drf_llm_gateway-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 974466c23f319db34908e800352b232728f59fe675e4c01ccb1ccc007525a647
MD5 aeb66eec5c0884991929fc0c3307cae6
BLAKE2b-256 3bc1b67dfbd01b856225a71ac33116beb6e5faa47077f4be51b4dab71183bb8d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page