-
Notifications
You must be signed in to change notification settings - Fork 3
Jyh/sse endpoint #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gargameljyh
wants to merge
5
commits into
main
Choose a base branch
from
jyh/sse-endpoint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Jyh/sse endpoint #53
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
26d04dd
feat(mcp-connector): add sse endpoint
gargameljyh e6a78ad
feat(mcp-connector): use http connect replace express
gargameljyh 139ec19
docs(mcp-connector): add sse connector docs
gargameljyh 55c766f
docs(mcp-connect): fix coderabbit suggestion
gargameljyh a024756
docs(mcp-connect): options request handle request headers
gargameljyh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
VITE_CONNECTOR_ENDPOINT_URL=ws://localhost:3001/ws | ||
VITE_CONNECTOR_WS_ENDPOINT_URL=ws://localhost:3001/ws | ||
VITE_CONNECTOR_SSE_ENDPOINT_URL=http://localhost:8082/client | ||
VITE_CHAT_URL=http://localhost:3001/chat |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,245 @@ | ||
# 自定义Connector | ||
|
||
本章节主要介绍如何实现一个自定义的通信层Connector,用以客户端与MCP服务端之间进行通信访问 | ||
|
||
## 自定义服务端 | ||
|
||
服务端通信需要以 TinyAgent 规定的 IConnectorEndpoint 协议为基础,当然客户端也是基于此协议进行拓展。 | ||
IConnectorEndpoint 协议要求服务端关注消息的接收发送以及启动(初始化)服务端实例,以下是 IConnectorEndpoint 协议的内容 | ||
|
||
```typescript | ||
interface IConnectorEndpoint { | ||
clientId: string | number; | ||
clientIdResolved: Promise<string | number>; | ||
|
||
start(): Promise<void>; | ||
close(): Promise<void>; | ||
|
||
send(message: IEndpointMessage): Promise<void>; | ||
onmessage?: ((message: IEndpointMessage) => void) | null; | ||
|
||
onclose?: (() => void) | null; | ||
onerror?: ((error: Error) => void) | null; | ||
} | ||
``` | ||
|
||
接下来基于 IConnectorEndpoint 协议实现服务端,服务端主要关注于通信,通信方式使用SSE通信方法 | ||
|
||
```typescript | ||
import * as http from 'node:http'; | ||
import { JSONRPCMessage } from '@modelcontextprotocol/sdk/types'; | ||
import { EndpointMessageType, IConnectorEndpoint, IEndpointMessage } from '../endpoint.type'; | ||
|
||
class SSEServerEndpoint implements IConnectorEndpoint { | ||
protected app: http.Server; | ||
protected res: http.ServerResponse; | ||
public clientId: string; | ||
public clientIdResolved: Promise<string>; | ||
|
||
constructor(app: http.Server, res: any, clientId: string) { | ||
this.app = app; | ||
this.res = res; | ||
this.clientId = clientId; | ||
this.clientIdResolved = Promise.resolve(clientId); | ||
} | ||
|
||
async start(): Promise<void> { | ||
// 订阅http请求 | ||
this.app.on('request', (req, res) => { | ||
if (req.method === 'OPTIONS' && req.url === '/message') { | ||
res.writeHead(204, { | ||
'Access-Control-Allow-Origin': '*', | ||
'Access-Control-Allow-Methods': '*', | ||
'Access-Control-Allow-Headers': '*', | ||
}); | ||
return res.end(); | ||
} | ||
// 定义/message api 用以接收客户端的请求内容 | ||
if (req.url === '/message') { | ||
// 解决跨域问题 | ||
res.setHeader('Access-Control-Allow-Origin', '*'); | ||
res.setHeader('Access-Control-Allow-Methods', '*'); | ||
|
||
// 读取请求体 | ||
let body = ''; | ||
req.on('data', (chunk) => { | ||
body += chunk.toString(); | ||
}); | ||
|
||
// 响应请求 | ||
req.on('end', () => { | ||
try { | ||
const message = JSON.parse(body); | ||
if (message.type === EndpointMessageType.INITIALIZE) { | ||
res.end(); | ||
return; | ||
} | ||
|
||
this.onmessage?.(message); | ||
} finally { | ||
res.end(); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
// 关闭SSE连接 | ||
async close(): Promise<void> { | ||
this.res.end(); | ||
} | ||
// 使用SSE连接向客户端推送消息 | ||
async send(message: IEndpointMessage<JSONRPCMessage>): Promise<void> { | ||
this.res.write(`data: ${JSON.stringify(message)}\n\n`); | ||
} | ||
onmessage?: ((message: IEndpointMessage<JSONRPCMessage>) => void) | null | undefined; | ||
onclose?: (() => void) | null | undefined; | ||
onerror?: ((error: Error) => void) | null | undefined; | ||
} | ||
``` | ||
|
||
为使模块责任清晰,服务端的启动以及ClientId的初始化工作交给了Server模块,该模块并不负责通信,以下是Server模块的代码示例 | ||
|
||
```typescript | ||
import * as http from 'node:http'; | ||
import { ConnectorCenter } from '../connector-center'; | ||
import { EndpointMessageType } from '../endpoint.type'; | ||
import { genId } from '../utils'; | ||
import { SSEServerEndpoint } from './sse-server-endpoint'; | ||
|
||
class SSEEndpointServer { | ||
public app: http.Server; | ||
protected port: number; | ||
protected connectorCenter: ConnectorCenter<SSEServerEndpoint>; | ||
|
||
constructor(config: { port: number }, connectorCenter: ConnectorCenter<SSEServerEndpoint>) { | ||
// 启动http服务器 | ||
this.app = http.createServer(); | ||
this.port = config.port; | ||
this.connectorCenter = connectorCenter; | ||
} | ||
|
||
start() { | ||
this.app.listen(config.port); | ||
this.app.on('request', (req, res) => { | ||
if (req.method === 'OPTIONS' && req.url === '/client') { | ||
res.writeHead(204, { | ||
'Access-Control-Allow-Origin': '*', | ||
'Access-Control-Allow-Methods': '*', | ||
'Access-Control-Allow-Headers': '*', | ||
}); | ||
return res.end(); | ||
} | ||
// 定义/client api用以初始化ClientId | ||
if (req.url === '/client') { | ||
const clientId = genId(); | ||
const endpoint = new SSEServerEndpoint(this.app, res, clientId); | ||
|
||
endpoint.start(); | ||
|
||
this.connectorCenter.setClient(clientId, endpoint); | ||
|
||
// SSE方式通信 | ||
res.setHeader('Content-Type', 'text/event-stream'); | ||
res.setHeader('Cache-Control', 'no-cache'); | ||
res.setHeader('Connection', 'keep-alive'); | ||
// 解决跨域问题 | ||
res.setHeader('Access-Control-Allow-Origin', '*'); | ||
res.setHeader('Access-Control-Allow-Methods', '*'); | ||
// 推送初始化的ClientId | ||
res.write( | ||
'data: ' + | ||
JSON.stringify({ | ||
type: EndpointMessageType.INITIALIZE, | ||
data: { | ||
clientId, | ||
}, | ||
}) + | ||
'\n\n', | ||
); | ||
} | ||
}); | ||
} | ||
} | ||
``` | ||
|
||
## 自定义客户端 | ||
|
||
下面是SSE客户端的实现,同意基于IConnectorEndpoint 协议 | ||
|
||
```typescript | ||
import { JSONRPCMessage } from '@modelcontextprotocol/sdk/types'; | ||
import { EndpointMessageType, IConnectorEndpoint, IEndpointMessage } from '../endpoint.type'; | ||
|
||
class SSEClientEndpoint implements IConnectorEndpoint { | ||
public clientId!: string | number; | ||
public clientIdResolved: Promise<string | number>; | ||
protected clientIdResolver!: (id: string | number) => void; | ||
// SSE连接实例 | ||
protected eventSource!: EventSource; | ||
// SSE连接url | ||
protected url: string; | ||
// SSE连接配置项 | ||
protected config?: EventSourceInit; | ||
|
||
constructor(url: string, config?: EventSourceInit) { | ||
this.url = url; | ||
this.config = config; | ||
this.clientIdResolved = new Promise((resolve) => { | ||
this.clientIdResolver = resolve; | ||
}); | ||
} | ||
start(): Promise<void> { | ||
return new Promise((resolve) => { | ||
// 创建SSE连接实例 | ||
this.eventSource = new EventSource(this.url, this.config); | ||
|
||
this.eventSource.onopen = () => {}; | ||
|
||
this.eventSource.onerror = (error) => { | ||
console.error('SSE error:', error); | ||
this.onerror?.(error as any); | ||
}; | ||
|
||
// 接收服务端消息 | ||
this.eventSource.onmessage = (messageEvent: MessageEvent<string>) => { | ||
const message: IEndpointMessage = JSON.parse(messageEvent.data); | ||
if (message.type === EndpointMessageType.INITIALIZE) { | ||
this.clientId = (message.data as any).clientId; | ||
this.clientIdResolver(this.clientId); | ||
resolve(); | ||
return; | ||
} | ||
this.onmessage?.(message); | ||
}; | ||
}); | ||
} | ||
async close(): Promise<void> { | ||
this.eventSource.close(); | ||
this.onclose?.(); | ||
} | ||
// 由于SSE是用以服务端向客户端推送消息,后续客户端主动请求则是以HTTP进行 | ||
async send(message: IEndpointMessage<JSONRPCMessage>): Promise<void> { | ||
try { | ||
if (message.type !== EndpointMessageType.INITIALIZE) { | ||
await this.clientIdResolved; | ||
} | ||
|
||
const url = URL.parse(this.url); | ||
|
||
// 请求/message api 向服务端发送请求 | ||
fetch(`${url?.origin}/message`, { | ||
method: 'POST', | ||
body: JSON.stringify(message), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
} catch (e) { | ||
this.onerror?.(e as Error); | ||
} | ||
} | ||
onmessage?: ((message: IEndpointMessage<JSONRPCMessage>) => void) | null | undefined; | ||
onclose?: (() => void) | null | undefined; | ||
onerror?: ((error: Error) => void) | null | undefined; | ||
} | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.