Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
ai-code-interface.el/ai-code-mcp-http-server.el at main · liaohanqin/ai-code-interface.el · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
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
{{ message }}
liaohanqin
ai-code-interface.el
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
ai-code-interface.el
/
ai-code-mcp-http-server.el
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
228 lines (197 loc) · 8.48 KB
main
Breadcrumbs
ai-code-interface.el
/
ai-code-mcp-http-server.el
Copy path
Top
File metadata and controls
Code
Blame
228 lines (197 loc) · 8.48 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
;
;; ai-code-mcp-http-server.el --- HTTP transport for ai-code MCP tools -*-
lexical-binding
:
t
; -*-
;
; Author: Kang Tu <tninja@gmail.com>
;
; SPDX-License-Identifier: Apache-2.0
;
;; Commentary:
;
; Minimal local HTTP transport for
`ai-code-mcp-server'
.
;
;; Code:
(
require
'cl-lib
)
(
require
'json
)
(
require
'ai-code-mcp-server
)
(
defgroup
ai-code-mcp-http-server
nil
"
HTTP transport for AI Code MCP tools.
"
:group
'tools
:prefix
"
ai-code-mcp-http-server-
"
)
(
defcustom
ai-code-mcp-http-server-port
nil
"
Port used by the local MCP HTTP server.
When nil, an available port is selected automatically.
"
:type
'(choice (const
:tag
"
Auto-select
"
nil
)
integer)
:group
'ai-code-mcp-http-server
)
(
defvar
ai-code-mcp-http-server--server
nil
"
Server process for the local MCP HTTP transport.
"
)
(
defvar
ai-code-mcp-http-server--port
nil
"
Port for
`ai-code-mcp-http-server--server'
.
"
)
(
defun
ai-code-mcp-http-server-live-p
()
"
Return non-nil when the local MCP HTTP server is running.
"
(
and
ai-code-mcp-http-server--server
(
process-live-p
ai-code-mcp-http-server--server)
ai-code-mcp-http-server--port))
(
defun
ai-code-mcp-http-server-ensure
()
"
Start the local MCP HTTP server when needed and return its port.
"
(
unless
(ai-code-mcp-http-server-live-p)
(ai-code-mcp-http-server--start))
ai-code-mcp-http-server--port)
(
defun
ai-code-mcp-http-server-stop
()
"
Stop the local MCP HTTP server.
"
(
interactive
)
(
when
(
process-live-p
ai-code-mcp-http-server--server)
(
delete-process
ai-code-mcp-http-server--server))
(
setq
ai-code-mcp-http-server--server
nil
ai-code-mcp-http-server--port
nil
))
(
defun
ai-code-mcp-http-server--start
()
"
Start the local MCP HTTP server.
"
(
let
((server (
make-network-process
:name
"
ai-code-mcp-http-server
"
:server
t
:host
"
127.0.0.1
"
:service
(
or
ai-code-mcp-http-server-port
0
)
:noquery
t
:log
#
'ai-code-mcp-http-server--accept
)))
(
setq
ai-code-mcp-http-server--server server
ai-code-mcp-http-server--port (
process-contact
server
:service
))))
(
defun
ai-code-mcp-http-server--accept
(
_server
client
_message
)
"
Initialize accepted CLIENT process.
"
(
set-process-query-on-exit-flag
client
nil
)
(
set-process-buffer
client (
generate-new-buffer
"
*ai-code-mcp-http-client*
"
))
(
set-process-coding-system
client
'binary
'binary
)
(
process-put
client
:data
"
"
)
(
set-process-filter
client
#
'ai-code-mcp-http-server--filter
)
(
set-process-sentinel
client
#
'ai-code-mcp-http-server--client-sentinel
))
(
defun
ai-code-mcp-http-server--client-sentinel
(
process
_event
)
"
Clean up PROCESS buffer after the client disconnects.
"
(
when
(
and
(
not
(
process-live-p
process))
(
buffer-live-p
(
process-buffer
process)))
(
kill-buffer
(
process-buffer
process))))
(
defun
ai-code-mcp-http-server--filter
(
process
chunk
)
"
Accumulate CHUNK for PROCESS and handle a full request.
"
(
let*
((data (
concat
(
or
(
process-get
process
:data
)
"
"
) chunk))
(request (ai-code-mcp-http-server--parse-request data)))
(
process-put
process
:data
data)
(
when
request
(
process-put
process
:data
nil
)
(ai-code-mcp-http-server--handle-request process request))))
(
defun
ai-code-mcp-http-server--parse-request
(
data
)
"
Parse DATA when it includes a full HTTP request.
"
(
when
(
string-match
"
\r\n\r\n
"
data)
(
let*
((separator-start (
match-beginning
0
))
(body-start (
match-end
0
))
(header-text (
substring
data
0
separator-start))
(lines (
split-string
header-text
"
\r\n
"
t
))
(request-line (
car
lines))
(headers (
delq
nil
(
mapcar
#
'ai-code-mcp-http-server--parse-header
(
cdr
lines))))
(content-length (
string-to-number
(
or
(
cdr
(
assoc
"
content-length
"
headers))
"
0
"
))))
(
when
(
<=
(
+
body-start content-length) (
string-bytes
data))
(
pcase-let
((`(
,method
,path
)
(ai-code-mcp-http-server--parse-request-line request-line)))
(
list
:method
method
:path
path
:headers
headers
:body
(
substring
data body-start (
+
body-start content-length))))))))
(
defun
ai-code-mcp-http-server--parse-request-line
(
line
)
"
Parse HTTP request LINE.
"
(
let
((parts (
split-string
line
"
"
t
)))
(
unless
(
>=
(
length
parts)
2
)
(
error
"
Malformed request line
"
))
(
list
(
nth
0
parts) (
nth
1
parts))))
(
defun
ai-code-mcp-http-server--parse-header
(
line
)
"
Parse HTTP header LINE into a cons cell.
"
(
when
(
string-match
"
\\
`
\\
([^:]+
\\
):[
\t
]*
\\
(.*
\\
)
\\
'
"
line)
(
cons
(
downcase
(
match-string
1
line))
(
match-string
2
line))))
(
defun
ai-code-mcp-http-server--handle-request
(
process
request
)
"
Handle REQUEST received on PROCESS.
"
(
condition-case
err
(
pcase
(
plist-get
request
:method
)
(
"
POST
"
(ai-code-mcp-http-server--handle-post process request))
(_
(ai-code-mcp-http-server--send-response process
404
"
text/plain
"
"
Not Found
"
)))
(
error
(ai-code-mcp-http-server--send-json-error
process
(ai-code-mcp-http-server--request-id request)
-32603
(
format
"
Internal error:
%s
"
(
error-message-string
err))))))
(
defun
ai-code-mcp-http-server--handle-post
(
process
request
)
"
Handle POST REQUEST on PROCESS.
"
(
let
((response
(ai-code-mcp-http-server--json-rpc-response
(
plist-get
request
:path
)
(
plist-get
request
:body
))))
(
if
(
null
response)
(ai-code-mcp-http-server--send-accepted process)
(ai-code-mcp-http-server--send-json
process
200
response))))
(
defun
ai-code-mcp-http-server--json-rpc-response
(
path
body
)
"
Return a JSON-RPC response alist for PATH and BODY.
Returns nil for notifications.
"
(
let*
((json-object (json-parse-string body
:object-type
'alist
))
(id (
alist-get
'id
json-object))
(method (
alist-get
'method
json-object))
(params (
alist-get
'params
json-object))
(ai-code-mcp--current-session-id
(ai-code-mcp-http-server--session-id-from-path path)))
(
when
id
`((jsonrpc
.
"
2.0
"
)
(id
.
,id
)
(result
.
,(ai-code-mcp-dispatch method params))))))
(
defun
ai-code-mcp-http-server--request-id
(
request
)
"
Extract the JSON-RPC request ID from REQUEST.
"
(
when-let
((body (
plist-get
request
:body
)))
(
condition-case
nil
(
alist-get
'id
(json-parse-string body
:object-type
'alist
))
(
error
nil
))))
(
defun
ai-code-mcp-http-server--session-id-from-path
(
path
)
"
Extract session ID from PATH.
"
(
when
(
string-match
"
\\
`/mcp/
\\
([^/?]+
\\
)
"
path)
(
match-string
1
path)))
(
defun
ai-code-mcp-http-server--send-json-error
(
process
id
code
message
)
"
Send a JSON-RPC error response with ID, CODE, and MESSAGE on PROCESS.
"
(ai-code-mcp-http-server--send-json
process
500
`((jsonrpc
.
"
2.0
"
)
(id
.
,id
)
(
error
.
((code
.
,code
)
(
message
.
,message
))))))
(
defun
ai-code-mcp-http-server--send-json
(
process
code
payload
)
"
Send JSON PAYLOAD with HTTP CODE on PROCESS.
"
(
let
((json-object-type
'alist
)
(json-array-type
'list
)
(json-key-type
'symbol
))
(ai-code-mcp-http-server--send-response
process
code
"
application/json
"
(
json-encode
payload))))
(
defun
ai-code-mcp-http-server--send-accepted
(
process
)
"
Send an empty HTTP 202 Accepted response on PROCESS.
"
(ai-code-mcp-http-server--send-response process
202
"
text/plain
"
"
"
))
(
defun
ai-code-mcp-http-server--send-response
(
process
code
content-type
body
)
"
Send an HTTP response with CODE, CONTENT-TYPE, and BODY on PROCESS.
"
(
let*
((payload (
or
body
"
"
))
(response (
concat
(
format
"
HTTP/1.1
%d
%s
\r\n
"
code
(ai-code-mcp-http-server--reason code))
(
format
"
Content-Type:
%s
\r\n
"
content-type)
(
format
"
Content-Length:
%d
\r\n
"
(
string-bytes
payload))
"
Connection: close
\r\n\r\n
"
payload)))
(
process-send-string
process response)
(
delete-process
process)))
(
defun
ai-code-mcp-http-server--reason
(
code
)
"
Return the HTTP reason phrase for CODE.
"
(
alist-get
code '((
200
.
"
OK
"
)
(
202
.
"
Accepted
"
)
(
404
.
"
Not Found
"
)
(
500
.
"
Internal Server Error
"
))
"
OK
"
))
(
provide
'ai-code-mcp-http-server
)
;
;; ai-code-mcp-http-server.el ends here
You can’t perform that action at this time.