@gluneau/hive-mcp-server
Version:
An MCP server that enables AI assistants to interact with the Hive blockchain
229 lines • 10.6 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPost = createPost;
exports.createComment = createComment;
// Content creation tools implementation
const dhive_1 = require("@hiveio/dhive");
const client_1 = __importDefault(require("../config/client"));
const config_1 = __importDefault(require("../config"));
const error_1 = require("../utils/error");
const response_1 = require("../utils/response");
// Create a new blog post
function createPost(params) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
try {
// Get credentials from environment variables
const username = config_1.default.hive.username;
const postingKey = config_1.default.hive.postingKey;
if (!username || !postingKey) {
return (0, response_1.errorResponse)('Error: HIVE_USERNAME or HIVE_POSTING_KEY environment variables are not set.');
}
// Generate permalink if not provided
const finalPermalink = params.permalink ||
params.title
.toLowerCase()
.replace(/[^\w\s-]/g, '') // Remove non-word chars except spaces and hyphens
.replace(/\s+/g, '-') // Replace spaces with hyphens
.replace(/-+/g, '-') // Replace multiple hyphens with single hyphen
.slice(0, 255); // Restrict to 255 chars
// Ensure first tag is used as the main category
const finalTags = [...new Set(params.tags)]; // Remove duplicates
// Prepare the post operation
const postOperation = {
parent_author: '', // Empty for main posts (non-comments)
parent_permlink: finalTags[0], // First tag is the main category
author: username,
permlink: finalPermalink,
title: params.title,
body: params.body,
json_metadata: JSON.stringify({
tags: finalTags,
app: 'hive-mcp-server/1.0',
}),
};
// Prepare post options if needed
let hasOptions = false;
const options = {
author: username,
permlink: finalPermalink,
max_accepted_payout: params.max_accepted_payout || '1000000.000 HBD',
percent_hbd: (_a = params.percent_hbd) !== null && _a !== void 0 ? _a : 10000,
allow_votes: params.allow_votes,
allow_curation_rewards: params.allow_curation_rewards,
extensions: ((_b = params.beneficiaries) === null || _b === void 0 ? void 0 : _b.length)
? [
[
0,
{
beneficiaries: params.beneficiaries.map((b) => ({
account: b.account,
weight: b.weight,
})),
},
],
]
: [],
};
// Add optional parameters if provided
if (params.max_accepted_payout) {
options.max_accepted_payout = params.max_accepted_payout;
hasOptions = true;
}
if (params.percent_hbd !== undefined) {
options.percent_hbd = params.percent_hbd;
hasOptions = true;
}
// Add beneficiaries if provided
if (params.beneficiaries && params.beneficiaries.length > 0) {
options.extensions = [
[
0,
{
beneficiaries: params.beneficiaries.map((b) => ({
account: b.account,
weight: b.weight,
})),
},
],
];
hasOptions = true;
}
let result;
if (hasOptions) {
// Use commentWithOptions when we have options
result = yield client_1.default.broadcast.commentWithOptions(postOperation, options, dhive_1.PrivateKey.fromString(postingKey));
}
else {
// Use standard comment for basic posts
result = yield client_1.default.broadcast.comment(postOperation, dhive_1.PrivateKey.fromString(postingKey));
}
return (0, response_1.successJson)({
success: true,
transaction_id: result.id,
transaction_url: `https://www.hiveblockexplorer.com/tx/${result.id}`,
block_num: result.block_num,
author: username,
permlink: finalPermalink,
title: params.title,
tags: finalTags,
url: `https://hive.blog/@${username}/${finalPermalink}`,
});
}
catch (error) {
return (0, response_1.errorResponse)((0, error_1.handleError)(error, 'create_post'));
}
});
}
// Create a comment on an existing post or reply to a comment
function createComment(params) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
try {
// Get credentials from environment variables
const username = config_1.default.hive.username;
const postingKey = config_1.default.hive.postingKey;
if (!username || !postingKey) {
return (0, response_1.errorResponse)('Error: HIVE_USERNAME or HIVE_POSTING_KEY environment variables are not set.');
}
// Generate a random permalink if not provided
const finalPermalink = params.permalink ||
`re-${params.parent_permlink.slice(0, 20)}-${Date.now().toString(36)}`;
// Prepare the comment operation
const commentOperation = {
parent_author: params.parent_author,
parent_permlink: params.parent_permlink,
author: username,
permlink: finalPermalink,
title: '', // Comments don't have titles
body: params.body,
json_metadata: JSON.stringify({
app: 'hive-mcp-server/1.0',
}),
};
// Prepare comment options if needed
let hasOptions = false;
const options = {
author: username,
permlink: finalPermalink,
max_accepted_payout: params.max_accepted_payout || '1000000.000 HBD',
percent_hbd: (_a = params.percent_hbd) !== null && _a !== void 0 ? _a : 10000,
allow_votes: params.allow_votes,
allow_curation_rewards: params.allow_curation_rewards,
extensions: ((_b = params.beneficiaries) === null || _b === void 0 ? void 0 : _b.length)
? [
[
0,
{
beneficiaries: params.beneficiaries.map((b) => ({
account: b.account,
weight: b.weight,
})),
},
],
]
: [],
};
// Add optional parameters if provided
if (params.max_accepted_payout) {
options.max_accepted_payout = params.max_accepted_payout;
hasOptions = true;
}
if (params.percent_hbd !== undefined) {
options.percent_hbd = params.percent_hbd;
hasOptions = true;
}
// Add beneficiaries if provided
if (params.beneficiaries && params.beneficiaries.length > 0) {
options.extensions = [
[
0,
{
beneficiaries: params.beneficiaries.map((b) => ({
account: b.account,
weight: b.weight,
})),
},
],
];
hasOptions = true;
}
let result;
if (hasOptions) {
// Use commentWithOptions when we have options
result = yield client_1.default.broadcast.commentWithOptions(commentOperation, options, dhive_1.PrivateKey.fromString(postingKey));
}
else {
// Use standard comment for basic comments
result = yield client_1.default.broadcast.comment(commentOperation, dhive_1.PrivateKey.fromString(postingKey));
}
return (0, response_1.successJson)({
success: true,
transaction_id: result.id,
transaction_url: `https://www.hiveblockexplorer.com/tx/${result.id}`,
block_num: result.block_num,
parent_author: params.parent_author,
parent_permlink: params.parent_permlink,
author: username,
permlink: finalPermalink,
url: `https://hive.blog/@${params.parent_author}/${params.parent_permlink}#@${username}/${finalPermalink}`,
});
}
catch (error) {
return (0, response_1.errorResponse)((0, error_1.handleError)(error, 'create_comment'));
}
});
}
//# sourceMappingURL=content-creation.js.map