A typed Python SDK for building Cursor hooks
Project description
py-cursor-hooks
A typed Python Library for building Cursor hooks.
Why use this?
Cursor runs hooks as subprocesses and communicates via JSON on stdin/stdout. Without this SDK, you'd need to write shell scripts that manually parse JSON, validate inputs, and format responses. With it, you get to write Python and:
- Pydantic models for all hook inputs and outputs
- Type-safe interfaces that catch errors before runtime
- A CLI that works out of the box with Cursor
Quick Start
1. Install
uv add py-cursor-hooks
2. Write your hooks
Create a module with your hook implementations:
from hooks.interfaces import CursorHooks
from hooks.models import BeforeReadFileInput, BeforeReadFileOutput
class MyHooks(CursorHooks):
def before_read_file(
self, input: BeforeReadFileInput
) -> BeforeReadFileOutput:
# Block reading .env files
if input.file_path.endswith(".env"):
return BeforeReadFileOutput(
permission="deny",
user_message="Cannot read .env files",
agent_message="Reading .env files is not allowed",
)
return BeforeReadFileOutput(permission="allow")
hooks = MyHooks()
3. Register your hooks
In your pyproject.toml, add an entry point so the CLI can find your implementation:
[project.entry-points."py_cursor_hooks.hooks"]
default = "my_package.my_hooks:hooks"
Then install your project:
uv pip install -e .
4. Configure Cursor
Add your hooks to ~/.cursor/hooks.json:
{
"version": 1,
"hooks": {
"beforeReadFile": [
{ "command": "uvx --from /absolute/path/to/your/project python -m hooks.run --hook beforeReadFile" }
]
}
}
That's it! Cursor will now run your hooks automatically.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py_cursor_hooks-0.1.1.tar.gz.
File metadata
- Download URL: py_cursor_hooks-0.1.1.tar.gz
- Upload date:
- Size: 10.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
95f41cce34a848d32eebd7b1148bfa0d86880b774d2f29d237e1268363899e25
|
|
| MD5 |
c3f21a4e447351040a7379fa6c341d2b
|
|
| BLAKE2b-256 |
a66d43acedb21850e79e5d447ff5a777b59b361405203bdf6722d488f4972d2b
|
File details
Details for the file py_cursor_hooks-0.1.1-py3-none-any.whl.
File metadata
- Download URL: py_cursor_hooks-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
39cc913378c92db842b6c1424adeb701ef0ed1f22c2cd5d18085a757f5813a83
|
|
| MD5 |
e5bf188686af3dc8491d477f4e5648c0
|
|
| BLAKE2b-256 |
f9284353af1ca2bb95b00316ff738a94cf0419e6e8e3fa7c04ec8839cc831f0b
|