@cubicecho/discovery-core
v1.1.0
Published
The shared core of the cubicecho discovery packages: serving and fetching public discovery documents, and mDNS advertising and browsing on one responder per process.
Maintainers
Readme
@cubicecho/discovery-core
The shared core of the cubicecho discovery packages: serving and fetching public discovery documents, and advertising and browsing over mDNS.
Apps don't install this directly. They use
@cubicecho/mcp-discovery to be found as an MCP server, or
@cubicecho/graphql-mdns-discovery to be found as a
GraphQL API. Both are built on this package, so an app that uses them together gets one mDNS responder and
middleware that don't overlap.
Install
npm install @cubicecho/discovery-core zod
npm install bonjour-service # only for @cubicecho/discovery-core/mdnsNode 22 or later.
Quick start
Serve one public document on its own path, with CORS, caching and ETags handled:
import { createServer } from 'node:http';
import { answeringMiddleware, documentHandler, requestOrigin, requestPath } from '@cubicecho/discovery-core';
const serve = documentHandler('application/json', (request) => ({
specVersion: '1.0',
url: `${requestOrigin(request)}/api`,
}));
const discovery = answeringMiddleware(async (request, response) => {
const isMine = requestPath(request) === '/.well-known/example.json';
if (isMine) {
await serve(request, response);
}
return isMine;
});
createServer((request, response) => {
discovery(request, response, (error) => {
response.writeHead(error ? 500 : 404);
response.end();
});
}).listen(3000);Then curl http://localhost:3000/.well-known/example.json. In Express, it's app.use(discovery).
Why one responder per process
When bonjour-service stops a service, it also withdraws the address records that every other service on the host uses, so peers lose the address of a service that is still up. Here, everything in a process shares one responder, which puts the shared records straight back. Advertising on the LAN has the details.
Documentation
- llms.txt, the index of every page and export
- Install and entry points
- Serving a document
- Fetching a document
- Advertising on the LAN
- Browsing the LAN
- TXT records
- Building a discovery package
- Common mistakes
- Changelog
