django-admin-oauth-mcp 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- django_admin_oauth_mcp-0.1.0/LICENSE +21 -0
- django_admin_oauth_mcp-0.1.0/PKG-INFO +216 -0
- django_admin_oauth_mcp-0.1.0/README.md +192 -0
- django_admin_oauth_mcp-0.1.0/pyproject.toml +145 -0
- django_admin_oauth_mcp-0.1.0/pyproject.toml.orig +123 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/__init__.py +80 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/admin.py +91 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/apps.py +6 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/conf.py +213 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/forms.py +360 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/management/__init__.py +0 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/management/commands/__init__.py +0 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/management/commands/forget_unused_mcp_clients.py +21 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/protocol.py +321 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/py.typed +0 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/queries.py +309 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/rules.py +629 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/scopes.py +35 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/services.py +378 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/shows.py +38 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/templates/django_admin_oauth_mcp/_ticks.html +102 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/templates/django_admin_oauth_mcp/authorize.html +43 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/templates/django_admin_oauth_mcp/base.html +37 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/templates/django_admin_oauth_mcp/connection.html +23 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/templates/django_admin_oauth_mcp/connections.html +33 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/templates/django_admin_oauth_mcp/refused.html +14 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/tools.py +791 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/uploads.py +82 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/urls.py +54 -0
- django_admin_oauth_mcp-0.1.0/src/django_admin_oauth_mcp/views.py +309 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tirzono
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: django-admin-oauth-mcp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: An MCP server for Django that reads and changes exactly what the admin allows.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Classifier: Framework :: Django
|
|
8
|
+
Classifier: Framework :: Django :: 5.2
|
|
9
|
+
Classifier: Framework :: Django :: 6.0
|
|
10
|
+
Classifier: Framework :: Django :: 6.1
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Requires-Dist: django>=5.2
|
|
13
|
+
Requires-Dist: django-oauth-toolkit>=3.4
|
|
14
|
+
Requires-Dist: mcp-types>=2.3
|
|
15
|
+
Requires-Dist: pydantic>=2.12
|
|
16
|
+
Requires-Dist: structlog>=25.4
|
|
17
|
+
Requires-Dist: django-guardian>=3.0 ; extra == 'guardian'
|
|
18
|
+
Requires-Dist: pillow>=11.0 ; extra == 'images'
|
|
19
|
+
Requires-Python: >=3.12
|
|
20
|
+
Project-URL: Repository, https://github.com/Tirzono/django-admin-oauth-mcp
|
|
21
|
+
Provides-Extra: guardian
|
|
22
|
+
Provides-Extra: images
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# django-admin-oauth-mcp
|
|
26
|
+
|
|
27
|
+
An MCP server for Django. It lets Claude, or another MCP client, read and
|
|
28
|
+
change exactly what the Django admin lets the person who connected it, and
|
|
29
|
+
nothing more.
|
|
30
|
+
|
|
31
|
+
- `POST /mcp` speaks both eras of the protocol: the 2025 versions with the
|
|
32
|
+
`initialize` handshake, and 2026-07-28. Each request gets one JSON answer,
|
|
33
|
+
and there are no sessions, so any web process can answer any request.
|
|
34
|
+
What a message may hold is checked with `mcp-types`, the official SDK's
|
|
35
|
+
schema package.
|
|
36
|
+
- Signing in is OAuth 2.1 through django-oauth-toolkit, with dynamic client
|
|
37
|
+
registration and the RFC 8414 and RFC 9728 documents. Anybody may
|
|
38
|
+
register a client, but a client can only send people back to Claude's
|
|
39
|
+
redirect URIs (or the ones you list) or to their own machine.
|
|
40
|
+
- The rules are the admin's own. A model is readable if its admin would
|
|
41
|
+
let the person open its list. Its rows are the admin's `get_queryset`,
|
|
42
|
+
and its fields are the ones its pages show. Adding, changing and deleting
|
|
43
|
+
go through the admin's form, `save_model`, `delete_model` and log
|
|
44
|
+
entries. A new admin needs nothing here.
|
|
45
|
+
- The person ticks what a client may do, per admin section (application)
|
|
46
|
+
and kind: `orders.read`, `orders.write`, `orders.delete`.
|
|
47
|
+
- Credentials are never read: passwords, OAuth tokens, sessions, and
|
|
48
|
+
whatever you add.
|
|
49
|
+
|
|
50
|
+
- Rows the admin edits only inside another's page, as an inline, are added,
|
|
51
|
+
changed and deleted through that page: the inline's form and permissions,
|
|
52
|
+
and the parent's page saved with them.
|
|
53
|
+
- The admin list's actions run as ticking rows and choosing one would. One
|
|
54
|
+
that asks something first on a page of its own says what it asks, and
|
|
55
|
+
runs once answered. `NO_ACTIONS` keeps some to a person's click.
|
|
56
|
+
- Somebody changes what a connection may do at `/o/connections/`, without
|
|
57
|
+
connecting again.
|
|
58
|
+
|
|
59
|
+
The tools are `list_models`, `describe_model`, `query_records`, `get_record`
|
|
60
|
+
and `count_records`. With *Change* ticked somewhere, `describe_form`,
|
|
61
|
+
`create_record`, `update_record`, `list_actions`, `run_action`,
|
|
62
|
+
`list_object_permissions` and `set_object_permissions` are added (the last
|
|
63
|
+
two answer only for an admin with django-guardian's Object permissions page:
|
|
64
|
+
install the `[guardian]` extra). With *Delete* ticked somewhere,
|
|
65
|
+
`delete_record` is added.
|
|
66
|
+
|
|
67
|
+
A display method that draws a model's fields (a table of what was sent,
|
|
68
|
+
say) says which with `@shows`, so the tools read them as its page shows
|
|
69
|
+
them:
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
from django_admin_oauth_mcp import shows
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@shows("data", "before")
|
|
76
|
+
@admin.display(description="what changed")
|
|
77
|
+
def the_change(self, submission): ...
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Installing
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
uv add django-admin-oauth-mcp
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
With `[images]` to name pictures sent without an extension by what they are,
|
|
87
|
+
through Pillow, and `[guardian]` for the tools that say who may do what with
|
|
88
|
+
one record.
|
|
89
|
+
|
|
90
|
+
## Setting it up
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
# settings.py
|
|
94
|
+
from django_admin_oauth_mcp import oauth2_provider_settings
|
|
95
|
+
|
|
96
|
+
INSTALLED_APPS = [
|
|
97
|
+
"django.contrib.admin",
|
|
98
|
+
...
|
|
99
|
+
"oauth2_provider",
|
|
100
|
+
"django_admin_oauth_mcp", # after oauth2_provider, whose admins it replaces
|
|
101
|
+
]
|
|
102
|
+
|
|
103
|
+
OAUTH2_PROVIDER = oauth2_provider_settings("Shop")
|
|
104
|
+
|
|
105
|
+
DJANGO_ADMIN_OAUTH_MCP = {
|
|
106
|
+
"NAME": "shop",
|
|
107
|
+
"TITLE": "Shop",
|
|
108
|
+
"INSTRUCTIONS": "Shop is where our orders are kept. ...",
|
|
109
|
+
"SECRET_MODEL_FIELDS": {"customers.customer": ["api_key"]},
|
|
110
|
+
"MAX_UPLOAD_BYTES": 25 * 1024 * 1024,
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
# urls.py: at the root, where clients look for the discovery documents.
|
|
114
|
+
urlpatterns = [
|
|
115
|
+
...,
|
|
116
|
+
path("", include("django_admin_oauth_mcp.urls")),
|
|
117
|
+
]
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
`settings.LOGIN_URL` is where somebody signs in before saying yes. Then run
|
|
121
|
+
`python manage.py migrate` for django-oauth-toolkit's tables. Schedule
|
|
122
|
+
`python manage.py forget_unused_mcp_clients` (or `services.forget_unused()`
|
|
123
|
+
from a task queue) every night.
|
|
124
|
+
|
|
125
|
+
To connect from Claude, go to Settings, Connectors, *Add custom
|
|
126
|
+
connector*, and enter `https://<host>/mcp`, with no client ID or secret. In
|
|
127
|
+
Claude Code: `claude mcp add --transport http <name> https://<host>/mcp`,
|
|
128
|
+
then `/mcp` to sign in.
|
|
129
|
+
|
|
130
|
+
### `DJANGO_ADMIN_OAUTH_MCP`
|
|
131
|
+
|
|
132
|
+
Every key is optional.
|
|
133
|
+
|
|
134
|
+
| Key | Default | What it is |
|
|
135
|
+
|---|---|---|
|
|
136
|
+
| `NAME` | `"django"` | `serverInfo.name` |
|
|
137
|
+
| `TITLE` | `"Django"` | What the pages and the tools call the site ("the Shop admin") |
|
|
138
|
+
| `INSTRUCTIONS` | a sentence from `TITLE` | What the model is told the tools are for |
|
|
139
|
+
| `VERSION` | `settings.VERSION`, or this package's | `serverInfo.version` |
|
|
140
|
+
| `AUTHENTICATE` | none: this package's OAuth tokens | A callable, or its dotted path, given the request to `/mcp` and answering a `Caller(user, scopes)` or `None`, for a site that something in front of it already signs people in to. See below |
|
|
141
|
+
| `MAY_CONNECT` | active staff | A callable, or its dotted path, given the user. Only ever narrows: somebody inactive or signed out never connects |
|
|
142
|
+
| `ADMIN_SITE` | `django.contrib.admin.site` | The admin whose rules apply |
|
|
143
|
+
| `SECRET_APPS`, `SECRET_MODELS`, `SECRET_FIELDS` | none | Credentials besides the built-in ones: applications, `app.model`s, and field names on any model |
|
|
144
|
+
| `SECRET_MODEL_FIELDS` | none | `{"app.model": ["field"]}` |
|
|
145
|
+
| `NO_ACTIONS` | none | Admin actions never run, as `app.model.action`, besides allauth's address verifying and procrastinate's job actions |
|
|
146
|
+
| `MAX_UPLOAD_BYTES` | 10 MB | The biggest file a client may send |
|
|
147
|
+
| `REDIRECT_URIS` | Claude's | Exact redirect URIs a client may register, besides http on this machine |
|
|
148
|
+
| `ALLOWED_ORIGINS` | `settings.CORS_ALLOWED_ORIGINS` | Origins a browser page may call `/mcp` from, besides the site's own |
|
|
149
|
+
| `AUTHORIZE_TEMPLATE`, `REFUSED_TEMPLATE` | plain pages in this package | The page where somebody says yes, and the one for somebody who may not connect |
|
|
150
|
+
| `CONNECTIONS_TEMPLATE`, `CONNECTION_TEMPLATE` | plain pages in this package | Somebody's connected clients, and changing what one may do |
|
|
151
|
+
| `REGISTER_ADMIN` | `True` | Replace django-oauth-toolkit's admins with one list of connected clients |
|
|
152
|
+
| `ADMIN_CLASS` | `django.contrib.admin.ModelAdmin` | What that list is made from, such as `unfold.admin.ModelAdmin` |
|
|
153
|
+
|
|
154
|
+
The consent page's context has `title`, `sections` (each with `name` and
|
|
155
|
+
`ticks`), `kinds`, `every` (a tick per column for every section at once),
|
|
156
|
+
`returns_to` and the OAuth `form`. The connection page has the same grid,
|
|
157
|
+
and `application` and `error`. Both include
|
|
158
|
+
`templates/django_admin_oauth_mcp/_ticks.html`; copy the pages to start your own.
|
|
159
|
+
|
|
160
|
+
### Behind an identity-aware proxy
|
|
161
|
+
|
|
162
|
+
A site that should not be reachable from the internet at all can sit behind
|
|
163
|
+
a proxy that signs people in itself, such as Cloudflare Access with Managed
|
|
164
|
+
OAuth, where Access is the OAuth server an MCP client talks to. Access then
|
|
165
|
+
owns the `Authorization` header, so this package's tokens cannot be used.
|
|
166
|
+
`AUTHENTICATE` says who a request is from instead: verify what the proxy
|
|
167
|
+
sends (for Access, the `Cf-Access-Jwt-Assertion` JWT, against the team's keys
|
|
168
|
+
and the application's audience) and answer the person and what they may do.
|
|
169
|
+
|
|
170
|
+
```python
|
|
171
|
+
from django_admin_oauth_mcp import Caller
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def from_access(request) -> Caller | None:
|
|
175
|
+
email = verified_access_email(request) # None unless the JWT checks out
|
|
176
|
+
user = User.objects.filter(email=email).first() if email else None
|
|
177
|
+
return Caller(user=user, scopes=frozenset({"read"})) if user else None
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
The scopes are a token's: `{"read"}` reads every section and changes
|
|
181
|
+
nothing, and `{"orders.read", "orders.write"}` reads and changes one. There
|
|
182
|
+
is no page to tick them on, so the project decides. Who may connect
|
|
183
|
+
(`MAY_CONNECT`) is still asked on every request. Without the OAuth server,
|
|
184
|
+
only `path("mcp", django_admin_oauth_mcp.views.mcp)` needs mounting, and only
|
|
185
|
+
`/mcp` needs to be reachable through the proxy.
|
|
186
|
+
|
|
187
|
+
## Developing
|
|
188
|
+
|
|
189
|
+
```bash
|
|
190
|
+
uv sync
|
|
191
|
+
uv run poe check # ruff, mypy and the tests: what CI runs
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
The tests run against a small project in `tests/` (`tests/apps`) and need
|
|
195
|
+
Postgres, because reading runs in a read-only transaction with a statement
|
|
196
|
+
timeout. They use `localhost:5432` as `postgres`, or `POSTGRES_HOST`,
|
|
197
|
+
`POSTGRES_USER` and `POSTGRES_PASSWORD`.
|
|
198
|
+
|
|
199
|
+
A new protocol version is served by adding it to `HANDSHAKE_VERSIONS` or
|
|
200
|
+
`MODERN_VERSIONS` in `protocol.py` once `mcp-types` knows it and the tests
|
|
201
|
+
pass for it. Never by default.
|
|
202
|
+
|
|
203
|
+
## Releasing
|
|
204
|
+
|
|
205
|
+
Releases are automatic. Every push to `master` runs
|
|
206
|
+
[release-it](https://github.com/release-it/release-it) once lint and tests
|
|
207
|
+
pass. It reads the conventional commits
|
|
208
|
+
since the last tag and picks the next version: `fix:` is a patch, `feat:` a
|
|
209
|
+
minor, and a `BREAKING CHANGE:` footer a major. It then writes that version
|
|
210
|
+
into `pyproject.toml`, `uv.lock` and `CHANGELOG.md`, tags `v<version>`,
|
|
211
|
+
creates the GitHub release, and publishes the wheel to PyPI through trusted
|
|
212
|
+
publishing, so no token is stored anywhere. A push that is
|
|
213
|
+
only chores or docs releases nothing. Never set the version by hand.
|
|
214
|
+
|
|
215
|
+
`uv run poe next-version` prints what the next release would be, without
|
|
216
|
+
making one.
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
# django-admin-oauth-mcp
|
|
2
|
+
|
|
3
|
+
An MCP server for Django. It lets Claude, or another MCP client, read and
|
|
4
|
+
change exactly what the Django admin lets the person who connected it, and
|
|
5
|
+
nothing more.
|
|
6
|
+
|
|
7
|
+
- `POST /mcp` speaks both eras of the protocol: the 2025 versions with the
|
|
8
|
+
`initialize` handshake, and 2026-07-28. Each request gets one JSON answer,
|
|
9
|
+
and there are no sessions, so any web process can answer any request.
|
|
10
|
+
What a message may hold is checked with `mcp-types`, the official SDK's
|
|
11
|
+
schema package.
|
|
12
|
+
- Signing in is OAuth 2.1 through django-oauth-toolkit, with dynamic client
|
|
13
|
+
registration and the RFC 8414 and RFC 9728 documents. Anybody may
|
|
14
|
+
register a client, but a client can only send people back to Claude's
|
|
15
|
+
redirect URIs (or the ones you list) or to their own machine.
|
|
16
|
+
- The rules are the admin's own. A model is readable if its admin would
|
|
17
|
+
let the person open its list. Its rows are the admin's `get_queryset`,
|
|
18
|
+
and its fields are the ones its pages show. Adding, changing and deleting
|
|
19
|
+
go through the admin's form, `save_model`, `delete_model` and log
|
|
20
|
+
entries. A new admin needs nothing here.
|
|
21
|
+
- The person ticks what a client may do, per admin section (application)
|
|
22
|
+
and kind: `orders.read`, `orders.write`, `orders.delete`.
|
|
23
|
+
- Credentials are never read: passwords, OAuth tokens, sessions, and
|
|
24
|
+
whatever you add.
|
|
25
|
+
|
|
26
|
+
- Rows the admin edits only inside another's page, as an inline, are added,
|
|
27
|
+
changed and deleted through that page: the inline's form and permissions,
|
|
28
|
+
and the parent's page saved with them.
|
|
29
|
+
- The admin list's actions run as ticking rows and choosing one would. One
|
|
30
|
+
that asks something first on a page of its own says what it asks, and
|
|
31
|
+
runs once answered. `NO_ACTIONS` keeps some to a person's click.
|
|
32
|
+
- Somebody changes what a connection may do at `/o/connections/`, without
|
|
33
|
+
connecting again.
|
|
34
|
+
|
|
35
|
+
The tools are `list_models`, `describe_model`, `query_records`, `get_record`
|
|
36
|
+
and `count_records`. With *Change* ticked somewhere, `describe_form`,
|
|
37
|
+
`create_record`, `update_record`, `list_actions`, `run_action`,
|
|
38
|
+
`list_object_permissions` and `set_object_permissions` are added (the last
|
|
39
|
+
two answer only for an admin with django-guardian's Object permissions page:
|
|
40
|
+
install the `[guardian]` extra). With *Delete* ticked somewhere,
|
|
41
|
+
`delete_record` is added.
|
|
42
|
+
|
|
43
|
+
A display method that draws a model's fields (a table of what was sent,
|
|
44
|
+
say) says which with `@shows`, so the tools read them as its page shows
|
|
45
|
+
them:
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
from django_admin_oauth_mcp import shows
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@shows("data", "before")
|
|
52
|
+
@admin.display(description="what changed")
|
|
53
|
+
def the_change(self, submission): ...
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Installing
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
uv add django-admin-oauth-mcp
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
With `[images]` to name pictures sent without an extension by what they are,
|
|
63
|
+
through Pillow, and `[guardian]` for the tools that say who may do what with
|
|
64
|
+
one record.
|
|
65
|
+
|
|
66
|
+
## Setting it up
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
# settings.py
|
|
70
|
+
from django_admin_oauth_mcp import oauth2_provider_settings
|
|
71
|
+
|
|
72
|
+
INSTALLED_APPS = [
|
|
73
|
+
"django.contrib.admin",
|
|
74
|
+
...
|
|
75
|
+
"oauth2_provider",
|
|
76
|
+
"django_admin_oauth_mcp", # after oauth2_provider, whose admins it replaces
|
|
77
|
+
]
|
|
78
|
+
|
|
79
|
+
OAUTH2_PROVIDER = oauth2_provider_settings("Shop")
|
|
80
|
+
|
|
81
|
+
DJANGO_ADMIN_OAUTH_MCP = {
|
|
82
|
+
"NAME": "shop",
|
|
83
|
+
"TITLE": "Shop",
|
|
84
|
+
"INSTRUCTIONS": "Shop is where our orders are kept. ...",
|
|
85
|
+
"SECRET_MODEL_FIELDS": {"customers.customer": ["api_key"]},
|
|
86
|
+
"MAX_UPLOAD_BYTES": 25 * 1024 * 1024,
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
# urls.py: at the root, where clients look for the discovery documents.
|
|
90
|
+
urlpatterns = [
|
|
91
|
+
...,
|
|
92
|
+
path("", include("django_admin_oauth_mcp.urls")),
|
|
93
|
+
]
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
`settings.LOGIN_URL` is where somebody signs in before saying yes. Then run
|
|
97
|
+
`python manage.py migrate` for django-oauth-toolkit's tables. Schedule
|
|
98
|
+
`python manage.py forget_unused_mcp_clients` (or `services.forget_unused()`
|
|
99
|
+
from a task queue) every night.
|
|
100
|
+
|
|
101
|
+
To connect from Claude, go to Settings, Connectors, *Add custom
|
|
102
|
+
connector*, and enter `https://<host>/mcp`, with no client ID or secret. In
|
|
103
|
+
Claude Code: `claude mcp add --transport http <name> https://<host>/mcp`,
|
|
104
|
+
then `/mcp` to sign in.
|
|
105
|
+
|
|
106
|
+
### `DJANGO_ADMIN_OAUTH_MCP`
|
|
107
|
+
|
|
108
|
+
Every key is optional.
|
|
109
|
+
|
|
110
|
+
| Key | Default | What it is |
|
|
111
|
+
|---|---|---|
|
|
112
|
+
| `NAME` | `"django"` | `serverInfo.name` |
|
|
113
|
+
| `TITLE` | `"Django"` | What the pages and the tools call the site ("the Shop admin") |
|
|
114
|
+
| `INSTRUCTIONS` | a sentence from `TITLE` | What the model is told the tools are for |
|
|
115
|
+
| `VERSION` | `settings.VERSION`, or this package's | `serverInfo.version` |
|
|
116
|
+
| `AUTHENTICATE` | none: this package's OAuth tokens | A callable, or its dotted path, given the request to `/mcp` and answering a `Caller(user, scopes)` or `None`, for a site that something in front of it already signs people in to. See below |
|
|
117
|
+
| `MAY_CONNECT` | active staff | A callable, or its dotted path, given the user. Only ever narrows: somebody inactive or signed out never connects |
|
|
118
|
+
| `ADMIN_SITE` | `django.contrib.admin.site` | The admin whose rules apply |
|
|
119
|
+
| `SECRET_APPS`, `SECRET_MODELS`, `SECRET_FIELDS` | none | Credentials besides the built-in ones: applications, `app.model`s, and field names on any model |
|
|
120
|
+
| `SECRET_MODEL_FIELDS` | none | `{"app.model": ["field"]}` |
|
|
121
|
+
| `NO_ACTIONS` | none | Admin actions never run, as `app.model.action`, besides allauth's address verifying and procrastinate's job actions |
|
|
122
|
+
| `MAX_UPLOAD_BYTES` | 10 MB | The biggest file a client may send |
|
|
123
|
+
| `REDIRECT_URIS` | Claude's | Exact redirect URIs a client may register, besides http on this machine |
|
|
124
|
+
| `ALLOWED_ORIGINS` | `settings.CORS_ALLOWED_ORIGINS` | Origins a browser page may call `/mcp` from, besides the site's own |
|
|
125
|
+
| `AUTHORIZE_TEMPLATE`, `REFUSED_TEMPLATE` | plain pages in this package | The page where somebody says yes, and the one for somebody who may not connect |
|
|
126
|
+
| `CONNECTIONS_TEMPLATE`, `CONNECTION_TEMPLATE` | plain pages in this package | Somebody's connected clients, and changing what one may do |
|
|
127
|
+
| `REGISTER_ADMIN` | `True` | Replace django-oauth-toolkit's admins with one list of connected clients |
|
|
128
|
+
| `ADMIN_CLASS` | `django.contrib.admin.ModelAdmin` | What that list is made from, such as `unfold.admin.ModelAdmin` |
|
|
129
|
+
|
|
130
|
+
The consent page's context has `title`, `sections` (each with `name` and
|
|
131
|
+
`ticks`), `kinds`, `every` (a tick per column for every section at once),
|
|
132
|
+
`returns_to` and the OAuth `form`. The connection page has the same grid,
|
|
133
|
+
and `application` and `error`. Both include
|
|
134
|
+
`templates/django_admin_oauth_mcp/_ticks.html`; copy the pages to start your own.
|
|
135
|
+
|
|
136
|
+
### Behind an identity-aware proxy
|
|
137
|
+
|
|
138
|
+
A site that should not be reachable from the internet at all can sit behind
|
|
139
|
+
a proxy that signs people in itself, such as Cloudflare Access with Managed
|
|
140
|
+
OAuth, where Access is the OAuth server an MCP client talks to. Access then
|
|
141
|
+
owns the `Authorization` header, so this package's tokens cannot be used.
|
|
142
|
+
`AUTHENTICATE` says who a request is from instead: verify what the proxy
|
|
143
|
+
sends (for Access, the `Cf-Access-Jwt-Assertion` JWT, against the team's keys
|
|
144
|
+
and the application's audience) and answer the person and what they may do.
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
from django_admin_oauth_mcp import Caller
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def from_access(request) -> Caller | None:
|
|
151
|
+
email = verified_access_email(request) # None unless the JWT checks out
|
|
152
|
+
user = User.objects.filter(email=email).first() if email else None
|
|
153
|
+
return Caller(user=user, scopes=frozenset({"read"})) if user else None
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
The scopes are a token's: `{"read"}` reads every section and changes
|
|
157
|
+
nothing, and `{"orders.read", "orders.write"}` reads and changes one. There
|
|
158
|
+
is no page to tick them on, so the project decides. Who may connect
|
|
159
|
+
(`MAY_CONNECT`) is still asked on every request. Without the OAuth server,
|
|
160
|
+
only `path("mcp", django_admin_oauth_mcp.views.mcp)` needs mounting, and only
|
|
161
|
+
`/mcp` needs to be reachable through the proxy.
|
|
162
|
+
|
|
163
|
+
## Developing
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
uv sync
|
|
167
|
+
uv run poe check # ruff, mypy and the tests: what CI runs
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
The tests run against a small project in `tests/` (`tests/apps`) and need
|
|
171
|
+
Postgres, because reading runs in a read-only transaction with a statement
|
|
172
|
+
timeout. They use `localhost:5432` as `postgres`, or `POSTGRES_HOST`,
|
|
173
|
+
`POSTGRES_USER` and `POSTGRES_PASSWORD`.
|
|
174
|
+
|
|
175
|
+
A new protocol version is served by adding it to `HANDSHAKE_VERSIONS` or
|
|
176
|
+
`MODERN_VERSIONS` in `protocol.py` once `mcp-types` knows it and the tests
|
|
177
|
+
pass for it. Never by default.
|
|
178
|
+
|
|
179
|
+
## Releasing
|
|
180
|
+
|
|
181
|
+
Releases are automatic. Every push to `master` runs
|
|
182
|
+
[release-it](https://github.com/release-it/release-it) once lint and tests
|
|
183
|
+
pass. It reads the conventional commits
|
|
184
|
+
since the last tag and picks the next version: `fix:` is a patch, `feat:` a
|
|
185
|
+
minor, and a `BREAKING CHANGE:` footer a major. It then writes that version
|
|
186
|
+
into `pyproject.toml`, `uv.lock` and `CHANGELOG.md`, tags `v<version>`,
|
|
187
|
+
creates the GitHub release, and publishes the wheel to PyPI through trusted
|
|
188
|
+
publishing, so no token is stored anywhere. A push that is
|
|
189
|
+
only chores or docs releases nothing. Never set the version by hand.
|
|
190
|
+
|
|
191
|
+
`uv run poe next-version` prints what the next release would be, without
|
|
192
|
+
making one.
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "django-admin-oauth-mcp"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "An MCP server for Django that reads and changes exactly what the admin allows."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.12"
|
|
7
|
+
license = "MIT"
|
|
8
|
+
license-files = ["LICENSE"]
|
|
9
|
+
classifiers = [
|
|
10
|
+
"Framework :: Django",
|
|
11
|
+
"Framework :: Django :: 5.2",
|
|
12
|
+
"Framework :: Django :: 6.0",
|
|
13
|
+
"Framework :: Django :: 6.1",
|
|
14
|
+
"Programming Language :: Python :: 3",
|
|
15
|
+
]
|
|
16
|
+
dependencies = [
|
|
17
|
+
"django>=5.2",
|
|
18
|
+
"django-oauth-toolkit>=3.4",
|
|
19
|
+
"mcp-types>=2.3",
|
|
20
|
+
"pydantic>=2.12",
|
|
21
|
+
"structlog>=25.4",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[project.optional-dependencies]
|
|
25
|
+
images = ["pillow>=11.0"]
|
|
26
|
+
guardian = ["django-guardian>=3.0"]
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
Repository = "https://github.com/Tirzono/django-admin-oauth-mcp"
|
|
30
|
+
|
|
31
|
+
[build-system]
|
|
32
|
+
requires = ["uv_build>=0.8.17,<0.10"]
|
|
33
|
+
build-backend = "uv_build"
|
|
34
|
+
|
|
35
|
+
[dependency-groups]
|
|
36
|
+
dev = [
|
|
37
|
+
"django-guardian>=3.0",
|
|
38
|
+
"django-stubs[compatible-mypy]>=5.1",
|
|
39
|
+
"model-bakery>=1.20",
|
|
40
|
+
"mypy>=1.14",
|
|
41
|
+
"pillow>=11.0",
|
|
42
|
+
"poethepoet>=0.32",
|
|
43
|
+
"psycopg[binary]>=3.2",
|
|
44
|
+
"pytest>=8.3",
|
|
45
|
+
"pytest-django>=4.9",
|
|
46
|
+
"ruff>=0.9",
|
|
47
|
+
"time-machine>=2.16",
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
[tool.ruff]
|
|
51
|
+
line-length = 100
|
|
52
|
+
target-version = "py312"
|
|
53
|
+
|
|
54
|
+
[tool.ruff.lint]
|
|
55
|
+
select = [
|
|
56
|
+
"E",
|
|
57
|
+
"W",
|
|
58
|
+
"F",
|
|
59
|
+
"I",
|
|
60
|
+
"B",
|
|
61
|
+
"C4",
|
|
62
|
+
"DJ",
|
|
63
|
+
"UP",
|
|
64
|
+
"RUF",
|
|
65
|
+
]
|
|
66
|
+
ignore = [
|
|
67
|
+
"RUF012",
|
|
68
|
+
"B008",
|
|
69
|
+
]
|
|
70
|
+
|
|
71
|
+
[tool.ruff.lint.isort]
|
|
72
|
+
known-first-party = [
|
|
73
|
+
"django_admin_oauth_mcp",
|
|
74
|
+
"tests",
|
|
75
|
+
]
|
|
76
|
+
|
|
77
|
+
[tool.ruff.format]
|
|
78
|
+
docstring-code-format = true
|
|
79
|
+
|
|
80
|
+
[tool.mypy]
|
|
81
|
+
plugins = ["mypy_django_plugin.main"]
|
|
82
|
+
check_untyped_defs = true
|
|
83
|
+
warn_redundant_casts = true
|
|
84
|
+
warn_unused_ignores = true
|
|
85
|
+
no_implicit_optional = true
|
|
86
|
+
files = ["src"]
|
|
87
|
+
|
|
88
|
+
[[tool.mypy.overrides]]
|
|
89
|
+
module = ["oauth2_provider.*"]
|
|
90
|
+
ignore_missing_imports = true
|
|
91
|
+
|
|
92
|
+
[tool.django-stubs]
|
|
93
|
+
django_settings_module = "tests.settings"
|
|
94
|
+
|
|
95
|
+
[tool.pytest.ini_options]
|
|
96
|
+
DJANGO_SETTINGS_MODULE = "tests.settings"
|
|
97
|
+
pythonpath = ["."]
|
|
98
|
+
testpaths = ["tests"]
|
|
99
|
+
addopts = "--strict-markers"
|
|
100
|
+
|
|
101
|
+
[tool.poe.tasks.test]
|
|
102
|
+
help = "Run the tests (needs Postgres: DATABASE_URL, or postgres on localhost)"
|
|
103
|
+
cmd = "pytest"
|
|
104
|
+
|
|
105
|
+
[tool.poe.tasks.lint]
|
|
106
|
+
help = "Check style with ruff and types with mypy"
|
|
107
|
+
|
|
108
|
+
[[tool.poe.tasks.lint.sequence]]
|
|
109
|
+
cmd = "ruff check ."
|
|
110
|
+
|
|
111
|
+
[[tool.poe.tasks.lint.sequence]]
|
|
112
|
+
cmd = "ruff format --check ."
|
|
113
|
+
|
|
114
|
+
[[tool.poe.tasks.lint.sequence]]
|
|
115
|
+
cmd = "mypy"
|
|
116
|
+
|
|
117
|
+
[tool.poe.tasks.format]
|
|
118
|
+
help = "Format the code with ruff"
|
|
119
|
+
|
|
120
|
+
[[tool.poe.tasks.format.sequence]]
|
|
121
|
+
cmd = "ruff check --fix ."
|
|
122
|
+
|
|
123
|
+
[[tool.poe.tasks.format.sequence]]
|
|
124
|
+
cmd = "ruff format ."
|
|
125
|
+
|
|
126
|
+
[tool.poe.tasks.next-version]
|
|
127
|
+
help = "What the next release would be, without making one (needs Node)"
|
|
128
|
+
cmd = """
|
|
129
|
+
npx --yes
|
|
130
|
+
--package release-it@${RELEASE_IT_VERSION}
|
|
131
|
+
--package @release-it/conventional-changelog@${CONVENTIONAL_CHANGELOG_VERSION}
|
|
132
|
+
release-it --release-version"""
|
|
133
|
+
|
|
134
|
+
[tool.poe.tasks.next-version.env.RELEASE_IT_VERSION]
|
|
135
|
+
default = "21.1.0"
|
|
136
|
+
|
|
137
|
+
[tool.poe.tasks.next-version.env.CONVENTIONAL_CHANGELOG_VERSION]
|
|
138
|
+
default = "12.0.2"
|
|
139
|
+
|
|
140
|
+
[tool.poe.tasks.check]
|
|
141
|
+
help = "Lint and test: what CI runs"
|
|
142
|
+
sequence = [
|
|
143
|
+
"lint",
|
|
144
|
+
"test",
|
|
145
|
+
]
|