Skip to main content

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": "cursor-hooks --hook beforeReadFile" }]
  }
}

That's it! Cursor will now run your hooks automatically.

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

py_cursor_hooks-0.1.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

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

py_cursor_hooks-0.1.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file py_cursor_hooks-0.1.0.tar.gz.

File metadata

  • Download URL: py_cursor_hooks-0.1.0.tar.gz
  • Upload date:
  • Size: 10.4 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

Hashes for py_cursor_hooks-0.1.0.tar.gz
Algorithm Hash digest
SHA256 930bf6dd00c1a31c8aa22fabc1a4192bcb94f7def98d71544b5516e5b7be7004
MD5 7a6cb485a49a9ee43dd213ab1705305a
BLAKE2b-256 4c55cfe32f3095fe7d1f823028d5a7a7d24ab6ffc42fb976717138aa0e206ab3

See more details on using hashes here.

File details

Details for the file py_cursor_hooks-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: py_cursor_hooks-0.1.0-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

Hashes for py_cursor_hooks-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2a531e96c1cc782d73b48163cf6bc7576c8ea8ab1956a09b1025c6d04ec69bf4
MD5 50407f11b649d5d72aab362c0f85d745
BLAKE2b-256 69341ac97c4763a4bb07fe6117b3ce77433296b150e151c8be13a13451f3558b

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