You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -6,12 +6,163 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
6
6
7
7
This is an Emacs MCP (Model Context Protocol) Server implementation written in pure Elisp. It enables direct integration between Large Language Models and Emacs internals by exposing Emacs functionality through standardized MCP tools.
8
8
9
-
## Code generation.
9
+
## Code Generation
10
10
11
-
- ALWAYS ensure that parantheses are perfectly balanced!
11
+
- ALWAYS ensure that parentheses are perfectly balanced!
12
12
- Be as concise as possible.
13
13
- Be extremely careful with the protocol and transport layers code. Always test code if making changes to it.
14
-
- Follow idiomatic ELisp conventions.
14
+
- Follow idiomatic Elisp conventions.
15
+
16
+
## Elisp Code Conventions
17
+
18
+
### Naming Conventions
19
+
20
+
**Private symbols use double-dash prefix:**
21
+
```elisp
22
+
;; Public API
23
+
(defun mcp-server-start () ...)
24
+
(defvar mcp-server-running nil)
25
+
26
+
;; Private/internal (not for external use)
27
+
(defun mcp-server--handle-message () ...)
28
+
(defvar mcp-server--client-counter 0)
29
+
```
30
+
31
+
**Package prefixes are mandatory:**
32
+
- All symbols must start with `mcp-server-` (or module-specific like `mcp-server-tools-`)
33
+
- This prevents namespace pollution
34
+
35
+
### Variables: defcustom vs defvar
36
+
37
+
**Use `defcustom` for user-configurable settings:**
38
+
```elisp
39
+
(defcustom mcp-server-debug nil
40
+
"Whether to enable debug logging."
41
+
:type 'boolean
42
+
:group 'mcp-server)
43
+
```
44
+
45
+
**Use `defvar` only for internal state:**
46
+
```elisp
47
+
(defvar mcp-server-running nil
48
+
"Whether the MCP server is currently running.")
49
+
```
50
+
51
+
**All defcustom must include:**
52
+
-`:type` specification
53
+
-`:group 'mcp-server` (or appropriate subgroup)
54
+
- Descriptive docstring
55
+
56
+
### String Comparisons
57
+
58
+
**Always use `string=` for string equality:**
59
+
```elisp
60
+
;; Correct
61
+
(when (string= method "initialize") ...)
62
+
63
+
;; Avoid for strings (works but less explicit)
64
+
(when (equal method "initialize") ...)
65
+
```
66
+
67
+
### Error Handling
68
+
69
+
**Standard pattern uses `condition-case`:**
70
+
```elisp
71
+
(condition-case err
72
+
(do-something-risky)
73
+
(error
74
+
(handle-error (error-message-string err))))
75
+
```
76
+
77
+
**Use `throw/catch` only for non-local exit (not errors):**
78
+
```elisp
79
+
;; Used in message handler for early exit after successful send
80
+
(catch 'mcp-handled
81
+
(when success
82
+
(throw 'mcp-handled 'success))
83
+
(fallback-action))
84
+
```
85
+
86
+
**Signal errors with `error`:**
87
+
```elisp
88
+
(unless tool
89
+
(error "Tool not found: %s" name))
90
+
```
91
+
92
+
### JSON Schema Conventions
93
+
94
+
**Use vectors for `required` fields:**
95
+
```elisp
96
+
;; Correct - vector
97
+
:input-schema '((type . "object")
98
+
(required . ["expression"]))
99
+
100
+
;; Wrong - list (won't serialize correctly)
101
+
:input-schema '((type . "object")
102
+
(required . ("expression")))
103
+
```
104
+
105
+
### Autoload Cookies
106
+
107
+
**Use `;;;###autoload` only for user-facing interactive commands:**
108
+
```elisp
109
+
;;;###autoload
110
+
(defun mcp-server-start () ...) ; User command - autoload
111
+
112
+
(defun mcp-server-main () ...) ; Internal entry point - no autoload
113
+
```
114
+
115
+
### Abstraction Boundaries
116
+
117
+
**Never call private (`--`) functions from other modules:**
-`get-diagnostics` - Get flycheck/flymake diagnostics from project buffers
246
+
247
+
Tools can be selectively enabled via `mcp-server-emacs-tools-enabled`:
248
+
```elisp
249
+
(setq mcp-server-emacs-tools-enabled 'all) ; All tools (default)
250
+
(setq mcp-server-emacs-tools-enabled '(get-diagnostics)) ; Only diagnostics
251
+
```
94
252
95
253
## Security Model
96
254
@@ -150,17 +308,59 @@ if client.connect() and client.initialize():
150
308
## Development Workflow
151
309
152
310
### Adding New Tools
311
+
312
+
Tools use a **self-registration pattern**: each tool file registers itself at load time via `mcp-server-register-tool`. This eliminates manual registry maintenance.
313
+
314
+
**Step 1:** Create a new file in `tools/` directory:
0 commit comments